gpt4 book ai didi

javascript - 使用javascript匹配innerHTML内容

转载 作者:行者123 更新时间:2023-12-02 14:24:00 26 4
gpt4 key购买 nike

我正在制作一款纸牌配对游戏。我将其设置为当我单击该框时将背景颜色更改为绿色。我如何让它检查第二张卡的内容(它也会变成绿色),如果它们匹配,则使它们不可见?

        <style type="text/css">
.box {
background-color: black;
width: 100px;
height: 100px;
margin: 10px;
line-height: 100px;
color:white;
font-size: 48;
font-family: helvetica;
text-align: center;
display: inline-block;
}
</style>
</head>
<body>
<div class="box">Bacon</div>
<div class="box">Waffle</div>
<div class="box">Toast</div> <br>

<div class="box">Coffee</div>
<div class="box">Eggs</div>
<div class="box">Oatmeal</div> <br>

<div class="box">Eggs</div>
<div class="box">Toast</div>
<div class="box">pancakes</div><br>

<div class="box">Waffle</div>
<div class="box">Oatmeal</div>
<div class="box">Bacon</div><br>


<script type="text/javascript">
var boxes = document.getElementsByClassName('box');
for (var i=0; i< boxes.length; i++){
boxes[i].style.backgroundColor = "black";
}
boxes[0].onclick = function(){
boxes[0].style.backgroundColor = "green";
}
boxes[1].onclick = function(){
boxes[1].style.backgroundColor = "green";
}
boxes[2].onclick = function(){
boxes[2].style.backgroundColor = "green";
}
boxes[3].onclick = function(){
boxes[3].style.backgroundColor = "green";
}
boxes[4].onclick = function(){
boxes[4].style.backgroundColor = "green";
}
boxes[5].onclick = function(){
boxes[5].style.backgroundColor = "green";
}
boxes[6].onclick = function(){
boxes[6].style.backgroundColor = "green";
}
boxes[7].onclick = function(){
boxes[7].style.backgroundColor = "green";
}
boxes[8].onclick = function(){
boxes[8].style.backgroundColor = "green";
}
boxes[9].onclick = function(){
boxes[9].style.backgroundColor = "green";
}
boxes[10].onclick = function(){
boxes[10].style.backgroundColor = "green";
}
boxes[11].onclick = function(){
boxes[11].style.backgroundColor = "green";
}

最佳答案

尝试这样的事情:

var boxes = document.getElementsByClassName('box');

// remeber the last box clicked
var _lastClicked = null;

for (var i=0; i< boxes.length; i++){
boxes[i].style.backgroundColor = "black";
boxes[i].addEventListener('click', onBox_click);
}

function onBox_click(domEvent){
// clicked element
var clicked = domEvent.target;

// prevent clicking on the same element
if (_lastClicked && _lastClicked === clicked)
return;

// if there is a box clicked and if the value match
if (_lastClicked && clicked.innerHTML === _lastClicked.innerHTML){
// the two boxes should disappear and we reset last clicked
_lastClicked.style.opacity = 0;
clicked.style.opacity = 0;
_lastClicked = null;
}

// if there is a box clicked and if the value does not match
if (_lastClicked && clicked.innerHTML !== _lastClicked.innerHTML){
// reset the color of the last clicked to black
_lastClicked.style.backgroundColor = "black";
}

_lastClicked = clicked;
clicked.style.backgroundColor = "green";
}
.box {
background-color: black;
width: 100px;
height: 100px;
margin: 10px;
line-height: 100px;
color:white;
font-size: 48;
font-family: helvetica;
text-align: center;
display: inline-block;
}
<div class="box">Bacon</div>
<div class="box">Waffle</div>
<div class="box">Toast</div> <br>

<div class="box">Coffee</div>
<div class="box">Eggs</div>
<div class="box">Oatmeal</div> <br>

<div class="box">Eggs</div>
<div class="box">Toast</div>
<div class="box">pancakes</div><br>

<div class="box">Waffle</div>
<div class="box">Oatmeal</div>
<div class="box">Bacon</div><br>

我删除了所有无用的代码行来监听框上的点击。现在更容易使用。

基本上,我只记得在 _lastClicked 变量中单击的最后一个框,并将其与单击的新框进行比较。如果匹配,我会让它们消失并重置 _lastClicked 变量。如果不匹配,我将 _lastClicked 的背景颜色重置为黑色。

希望对您有帮助;)

PS:我还确保单击的元素与以前不同。如果不这样做,在同一个元素上单击两次将会使其消失:p

关于javascript - 使用javascript匹配innerHTML内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38441491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com