Javascript:如何检查div中是否存在相同的数字,然后更改那个2或3个或更多div的背景颜色? [英] Javascript: how to check if exists the same numbers in div, then change that 2 or 3 or more divs background color?
本文介绍了Javascript:如何检查div中是否存在相同的数字,然后更改那个2或3个或更多div的背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我身体有4x4 div,当我点击任何div时,它会在div 0-100数字中生成随机数,就像这个 http: //prntscr.com/k5p0hr [ ^ ]
如果数字一致,我需要用任何颜色绘制。例如,如果div中有三个26个数字,那么应该更改三个div的背景颜色。
我尝试了什么:
我试过这样,但这是错的。
I have 4x4 divs in the body,when i click on any div, it's generating random numbers in that div 0-100 numbers, like this http://prntscr.com/k5p0hr[^]
If numbers are coinciding, i need to draw with any color that divs. For example if there are three 26 numbers in div, then that three div's background color should be changed.
What I have tried:
I have tried like this, but it's wrong.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Das 7</title>
<style>
.parent{
overflow: hidden;
}
.d2{
float: left;
margin: 1px;
outline: 1px solid red;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div>
<div class="parent">
<div class="d2"></div>
<div class="d2"></div>
<div class="d2"></div>
<div class="d2"></div>
</div>
<div class="parent">
<div class="d2"></div>
<div class="d2"></div>
<div class="d2"></div>
<div class="d2"></div>
</div>
<div class="parent">
<div class="d2"></div>
<div class="d2"></div>
<div class="d2"></div>
<div class="d2"></div>
</div>
<div class="parent">
<div class="d2"></div>
<div class="d2"></div>
<div class="d2"></div>
<div class="d2"></div>
</div>
</div>
</body>
<script src="JS/script.js"></script>
</html>
var arr = document.querySelectorAll(".d2");
for(var i = 0; i < arr.length; i++){
arr[i].addEventListener("click", F);
}
function F(){
var r = parseInt(Math.random()*100);
this.innerHTML = r;
if(arr.innerHTML == r){
arr[i].style.background = "red";
}
}
推荐答案
根据此处发布的内容,需要完成更多工作。以下是一个示例: div bg color [ ^ ]
arr
是一个数组,你需要包含一个循环来迭代它中的对象。然后,如果存在相同的数字,您还需要添加一些逻辑来更新其他单元格的背景颜色。
Based on what posted here, more works are required. Here is an example: div bg color[^]
Thearr
is an array, you need to include a loop to iterate the object in it. Then you also need to add some logic to update the background color of other cells if the same number exists.
这篇关于Javascript:如何检查div中是否存在相同的数字,然后更改那个2或3个或更多div的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文