完整代码https://codepen.io/3noki/pen/xjyErQ?editors=0010
对于这种纸牌游戏,如果两张牌不匹配,则此功能将执行;
function unFlip() {
openedCardsList[1].parent().removeClass('open');
openedCardsList[1].parents.toggle('open');
openedCardsList = [];
console.log('unflipped')
}
这应该是取消翻转卡片,但它给我的错误无论我想在这里说什么,它的切换不是功能还是切换未定义等等。
如何从父元素中删除该类?是因为我之前将卡片推到了这个数组,它不能告诉它的父母是什么,因为它不再是一个元素而是数组中的对象?
openedCardsList.push(this.querySelector('i'));
答案 0 :(得分:2)
openedCardsList[1]
是本机DOM对象,而不是jQuery对象。如果要使用jQuery .parent()
方法,首先必须将对象传递给$
函数,使其成为jQuery对象:
$(openedCardsList[1]).parent().removeClass('open');
答案 1 :(得分:-1)
检查以下代码段中的第3行。
/*declaring variables*/
//Create a list that holds all of your cards
let cardsList = [
"fa-diamond",
"fa-paper-plane-o",
"fa-anchor",
"fa-bolt",
"fa-cube",
"fa-leaf",
"fa-bicycle",
"fa-bomb",
"fa-diamond",
"fa-paper-plane-o",
"fa-anchor",
"fa-bolt",
"fa-cube",
"fa-leaf",
"fa-bicycle",
"fa-bomb"
];
restart = $(".restart");
deck = $(".deck");
score = $(".score-panel");
container = $(".container");
star = $(".fa-star");
moves = $(".moves");
timer = $(".timer");
cards = $(".card");
card = document.getElementsByClassName('card');
let openedCardsList = [];
let matchedCards = [];
let start = false;
let started = false;
let seconds = 0;
let clicks = 0;
let solvedCount = 0;
let time = setInterval(incrementSeconds, 1000);
/*declaring functions*/
//Shuffle function from http://stackoverflow.com/a/2450976
function shuffle(array) {
var currentIndex = array.length,
temporaryValue,
randomIndex;
while (currentIndex !== 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function init() {
let shuffledCards = shuffle(cardsList);
openedCardsList = [];
createCardHTML();
incrementSeconds();
}
function newGame() {
//cardFlip();
symbol = cards.children("i");
symbol.removeClass(
"fa-diamond fa-paper-plane-o fa-anchor fa-bolt fa-cube fa-leaf fa-bicycle fa-bomb fa-bug"
);
init();
clicks = 0;
moves.text(clicks);
cards.removeClass('open');
openedCardsList = [];
createCardHTML();
incrementSeconds();
}
function createCardHTML() {
symbol = cards.children("i");
symbol.each(function(index, item) {
$(item).addClass(cardsList[index]);
});
return symbol;
}
$(".card").on("click", function cardFlip() {
if (!this.classList.contains('open') && openedCardsList.length < 2) {
$(this).toggleClass('open');
e.preventDefault();
//this.classList.toggle('show');
openedCardsList.push(this.querySelector('i'));
console.log(openedCardsList);
console.log("flipped")
} else {
checkForMatch(event)
}
});
function checkForMatch(event) {
if (openedCardsList[0] === openedCardsList[1]) {
openedCardsList[0].classList.remove('open');
openedCardsList[0].classList.add('match');
openedCardsList[1].classList.remove('open');
openedCardsList[1].classList.add('match');
console.log('matched');
openedCardsList = [];
} else {
unFlip(event)
}
}
function unFlip() {
openedCardsList[0].classList.toggle(['open']);
openedCardsList[1].parents.toggle('open');
openedCardsList = [];
console.log('unflipped')
}
//counts clicks and edits text
$(".card").on("click", clickCounter);
function clickCounter() {
clicks++;
moves.text(clicks);
}
//counts seconds and displays
function incrementSeconds() {
if (matchedCards != 8) {
seconds += 1;
timer.text(seconds);
return;
}
//supposed to stop timer if game is over
else {
timer.text(seconds) = Number(countMatches).toLocaleString('en');
alert(Number);
}
}
//starts game when page is ready
$(document).ready(init);
//restarts on click
$(".restart").click(newGame);
html {
box-sizing: border-box;
}
*,
*::before,
*::after {
box-sizing: inherit;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
body {
background: #ffffff url('../img/geometry2.png');
/* Background pattern from Subtle Patterns */
font-family: 'Coda', cursive;
}
.container {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}
h1 {
font-family: 'Open Sans', sans-serif;
font-weight: 300;
}
/*
* Styles for the deck of cards
*/
.deck {
width: 660px;
min-height: 680px;
//background: linear-gradient(160deg, #02ccba 0%, #aa7ecd 100%);
padding: 32px;
border-radius: 10px;
box-shadow: 12px 15px 20px 8px rgba(46, 61, 73, 0.5);
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
margin: 0 0 3em;
}
.deck .card {
height: 125px;
width: 125px;
background: #070707;
font-size: 0;
color: #ffffff;
border-radius: 8px;
cursor: pointer;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 5px 2px 20px 0 rgba(46, 61, 73, 0.5);
}
.deck .card.open {
/* transform: rotateY(0); */
background: #81a37a;
cursor: default;
transform: rotateY(180deg);
transition: 1s ease;
}
.deck .card.open {
font-size: 33px;
transform: rotateY(180deg);
transition: 1s ease;
}
.deck .card.hide {
transform: rotateY(180deg);
transition: 1s ease;
}
.deck .card.match {
cursor: default;
background: #456848;
font-size: 33px;
}
/*
* Styles for the Score Panel
*/
.score-panel {
text-align: left;
width: 345px;
margin-bottom: 10px;
}
.score-panel .stars {
margin: 0;
padding: 0;
display: inline-block;
margin: 0 5px 0 0;
}
.score-panel .stars li {
list-style: none;
display: inline-block;
}
.score-panel .restart {
float: right;
cursor: pointer;
}
/* alert styles */
.alert {
padding: 20px;
background-color: #f44336;
color: white;
}
.closebtn {
margin-left: 15px;
color: white;
font-weight: bold;
float: left;
font-size: 22px;
line-height: 20px;
cursor: pointer;
transition: 0.3s;
}
.closebtn:hover {
color: black;
}
<html lang="en">
<head>
<meta charset="utf-8">
<title>Matching Game</title>
<meta name="description" content="">
<link rel="stylesheet prefetch" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css">
<link rel="stylesheet prefetch" href="https://fonts.googleapis.com/css?family=Coda">
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/app.css">
</head>
<body>
<div class="container">
<header>
<h1>Matching Game</h1>
</header>
<br/>
<section class="score-panel">
<ul class="stars">
<li><i id="1st-star" class="fa fa-star"></i></li>
<li><i id="2nd-star" class="fa fa-star"></i></li>
<li><i id="3rd-star" class="fa fa-star"></i></li>
</ul>
<span class="moves">0</span> :clicks | time:
<span class="timer">0</span> :seconds
<div class="restart"><i class="fa fa-repeat"></i></div>
</section>
<ul class="deck">
<li class="card">
<i class="fa "></i>
</li>
<li class="card">
<i class="fa "></i>
</li>
<li class="card">
<i class="fa "></i>
</li>
<li class="card">
<i class="fa "></i>
</li>
<li class="card">
<i class="fa "></i>
</li>
<li class="card">
<i class="fa "></i>
</li>
<li class="card">
<i class="fa "></i>
</li>
<li class="card">
<i class="fa "></i>
</li>
<li class="card">
<i class="fa "></i>
</li>
<li class="card">
<i class="fa "></i>
</li>
<li class="card">
<i class="fa "></i>
</li>
<li class="card">
<i class="fa "></i>
</li>
<li class="card">
<i class="fa "></i>
</li>
<li class="card">
<i class="fa "></i>
</li>
<li class="card">
<i class="fa "></i>
</li>
<li class="card">
<i class="fa "></i>
</li>
</ul>
</div>
<script src="js/app.js"></script>
</body>
</html>
并将其更改为: $(本).toggleClass( '开'); e.preventDefault();
$(".card").on("click", function cardFlip() {
if (!this.classList.contains('open') && openedCardsList.length < 2) {
$(this).toggleClass('open');
e.preventDefault();
//this.classList.toggle('show');
openedCardsList.push(this.querySelector('i'));
console.log(openedCardsList);
console.log("flipped")
} else {
checkForMatch(event)
}
});