我使用Javascript来创建625 <div class="box"><div>,然后为每个框添加事件侦听器。成功创建了这些框,但侦听器无法工作。在这里,我的完整代码,我真的很感谢您的所有反应。谢谢
<!DOCTYPE html>
<html>
<head>
<title>Number AI | Machine Learning Technologhy</title>
<style>
.box-container{
display: grid;
width: 300px;
height: 300px;
border: 1px solid blue;
grid-template-columns: auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto;
}
.box{
width:10px;
height:10px;
border: 1px solid black;
text-align: center;
}
</style>
<script type="text/javascript">
function initiateBox(){
for (let i = 0; i < 625; i++) {
let box = document.createElement("DIV");
box.classList.add("box");
document.querySelector('.box-container').appendChild(box);
}
};
window.onload = initiateBox;
</script>
</head>
<body>
<h2>Number AI</h2>
<div class="box-container"></div>
<script>
document.querySelectorAll(".box").forEach(box =>{
box.addEventListener("mouseover",function(){
box.style.backgroundColor = 'black';
});
});
</script>
</body>
</html>发布于 2020-10-03 08:15:39
你在这里有几个选择。但是,最简单的(不是高效的)是在框创建中添加事件侦听器,如下所示:
<!DOCTYPE html>
<html>
<head>
<title>Number AI | Machine Learning Technologhy</title>
<style>
.box-container {
display: grid;
width: 300px;
height: 300px;
border: 1px solid blue;
grid-template-columns: auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto;
}
.box {
width: 10px;
height: 10px;
border: 1px solid black;
text-align: center;
}
</style>
<script type="text/javascript">
function initiateBox() {
for (let i = 0; i < 625; i++) {
let box = document.createElement("DIV");
box.classList.add("box");
box.addEventListener("mouseover", function() {
box.style.backgroundColor = 'black';
});
document.querySelector('.box-container').appendChild(box);
}
};
window.onload = initiateBox;
</script>
</head>
<body>
<h2>Number AI</h2>
<div class="box-container"></div>
</body>
</html>
但是在任何情况下,如果您想找到一种高效的方法来做到这一点,正如@mplungjan在评论中所指出的,最好使用这样的代表团方法:
<!DOCTYPE html>
<html>
<head>
<title>Number AI | Machine Learning Technologhy</title>
<style>
.box-container {
display: grid;
width: 300px;
height: 300px;
border: 1px solid blue;
grid-template-columns: auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto;
}
.box {
width: 10px;
height: 10px;
border: 1px solid black;
text-align: center;
}
</style>
<script type="text/javascript">
function initiateBox() {
const boxContainer = document.querySelector('.box-container');
for (let i = 0; i < 625; i++) {
let box = document.createElement("DIV");
box.classList.add("box");
boxContainer.appendChild(box);
}
boxContainer.addEventListener("mouseover", function(event) {
const target = event.target;
if (target.classList.contains('box'))
target.style.backgroundColor = 'black';
});
};
window.onload = initiateBox;
</script>
</head>
<body>
<h2>Number AI</h2>
<div class="box-container"></div>
</body>
</html>
发布于 2020-10-03 08:13:47
几件事
总之,这是委托脚本
function initiateBox() {
const container = document.querySelector('.box-container');
for (let i = 0; i < 625; i++) {
let box = document.createElement("div");
box.classList.add("box");
container.appendChild(box);
}
container.addEventListener("mouseover", function(e) {
const tgt = e.target;
if (tgt.classList.contains("box")) tgt.style.backgroundColor = 'black';
});
};
window.addEventListener("load",initiateBox).box-container {
display: grid;
width: 300px;
height: 300px;
border: 1px solid blue;
grid-template-columns: auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto auto;
}
.box {
width: 10px;
height: 10px;
border: 1px solid black;
text-align: center;
}<title>Number AI | Machine Learning Technologhy</title>
<h2>Number AI</h2>
<div class="box-container">
</div>
发布于 2020-10-03 08:17:25
我认为第二个脚本是在向文档追加box元素之前运行的,因此找不到任何框。您可以将eventListener添加到您创建的initiateBox函数中,如下所示:
function initiateBox(){
for (let i = 0; i < 625; i++) {
let box = document.createElement("DIV");
box.classList.add("box");
box.addEventListener("mouseover",function(){
box.style.backgroundColor = 'black';
});
document.querySelector('.box-container').appendChild(box);
}
};
window.onload = initiateBox;如果背景色实际上是您要在mouseOver上更改的属性,我建议使用CSS悬停。
https://stackoverflow.com/questions/64182095
复制相似问题