使用javascript匹配innerHTML内容
Matching innerHTML Contents using javascript
我正在制作一个卡片匹配游戏。我已经设置好了,当我点击框时,将bg颜色更改为绿色。我如何让它检查第二张卡(也会变成绿色)的内容,如果它们匹配,则使它们不可见?
<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的背景色重置为黑色。
我希望它能有所帮助;)
附言:我还确保点击的元素与以前不同。如果不这样做,在同一个元素上单击两次将使其消失:p
没有jquery,只有纯JS
首先我们迭代和
boxes[i].style.backgroundColor = "black";
将bg颜色指定为黑色- CCD_ 2并附加回调函数
回叫FN
function clickFN() {
var elem = this,
style = elem.style;
if (lastClickedElem && elem === lastClickedElem) {
style.visibility = 'hidden';
}
style.backgroundColor = "green";
lastClickedElem = elem;
}
第一个CCD_ 3给出了对交互元素的引用。
- 检查是否存在任何lastClickedElem。
lastClickedElem && elem === lastClickedElem
- 如果存在,则同一元素将其隐藏。
style.visibility = 'hidden';
- 将背景颜色设置为绿色
- 更新lastClickedElem变量
var boxes = document.getElementsByClassName('box'),
lastClickedElem;
for (var i = 0; i < boxes.length; i++) {
boxes[i].style.backgroundColor = "black";
// attach the callback for the click interaction.
boxes[i].onclick = clickFN;
}
// callback function for the click event.
function clickFN() {
var elem = this,
style = elem.style;
if (lastClickedElem && elem === lastClickedElem) {
style.visibility = 'hidden';
}
style.backgroundColor = "green";
lastClickedElem = elem;
}
.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>
这看起来很有趣,我想出这个是为了让你开始。你还有一些逻辑需要弄清楚,我总是把最后一个点击的存储在那里,我想你想在错误的选择时重置。
此外,为了方便起见,我使用了jquery。
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 lastSelected;
$('.box').click(function(){
if(lastSelected && $(lastSelected).text() === $(this).text()){
console.log('Match')
$(lastSelected).addClass('hidden');
$(this).addClass('hidden');
}
lastSelected = $(this);
});
.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;
}
.hidden{
visibility: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<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>
相关文章:
- 如何通过搜索innerHTML的内容来获取HTML元素
- 如何使用innerHTML在javascript中附加内容
- 通过按键调整innerHTML后,内容可编辑分区变为只读
- 如何获取链接内容(innerHTML)并将其作为标题添加到其他链接
- 使用 innerHTML 和 vanilla JS 检索节点的内容
- innerHTML 不适用于 Javascript 输出内容
- 无法设置 jquery 对话框.InnerHtml(某些内容)无需刷新页面
- 我用JS在下拉框中更改了一些内容,但是innerHTML不会随更改的内容一起返回
- Javascript.innerHTML注释掉了以<开头的所有内容;
- 如何使用 jquery 使用 'data-id' 属性更改 innerHtml 内容
- 按键上的innerHTML-无法获取内容
- document.getElementById.innerHTML不写任何内容.
- 从函数返回内容时,将新创建的TD元素上的.innerHTML设置为空白
- 将内容可编辑文本的innerHTML转换为普通字符串
- 将innerHTML从列表中的一项复制到元素的内容中
- JavaScript使用for循环创建p标记,并填充innerHTML内容
- innerHTML不会改变span的内容
- 如何改变innerHTML内容,如(输入字段名称,id,onclick)属性
- jQuery . HTML()和.append()呈现的内容与HTML DOM innerHTML属性不同
- 使用html命令设置html内容.innerHTML,使其在ie8和ie9中工作