我正在创建一个基本的记忆游戏。如果第一个单击的 div 的 innerHTML 与第二个匹配,我想删除 div,但由于某种原因,无论单击哪两个 div,它都会删除 div。我该如何解决这个问题?
var picker = null;
var pickerTwo = null;
var count = 0;
function flip(event) {
if (count === 0) {
event.currentTarget.style.opacity = "1";
picker = event.currentTarget;
count++;
} else if (count === 1) {
event.currentTarget.style.opacity = "1";
pickerTwo = event.currentTarget;
if (picker.innerHTML === pickerTwo.innerHTML) {
setTimeout(function() {
var bigBoy = picker.parentNode;
var littleBoy = pickerTwo.parentNode;
bigBoy.removeChild(picker);
littleBoy.removeChild(pickerTwo);
bigBoy.parentNode.removeChild(bigBoy);
littleBoy.parentNode.removeChild(littleBoy);
}, 800);
} else if (picker.innerHTML != pickerTwo.innerHTML) {
setTimeout(function() {
picker.style.opacity = "0";
pickerTwo.style.opacity = "0";
}, 800);
}
count = 0;
}
}
.card2 {
background-color: #425dff;
width: 100px;
height: 100px;
margin-bottom: 10px;
}
<head>
<title>memory</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="theBoard">
<div id="case1">
<div class="card2" onclick="flip(event)">A</div>
</div>
<div id="case2">
<div class="card2" onclick="flip(event)">A</div>
</div>
<div id="case3">
<div class="card2" onclick="flip(event)">B</div>
</div>
<div id="case4">
<div class="card2" onclick="flip(event)">B</div>
</div>
</div>
<script src="js/app.js"></script>