【发布时间】:2020-03-21 05:38:45
【问题描述】:
我有一个 16x16 的小方块网格。我添加了一个永久的“悬停”效果,当我将鼠标放在第一个框上时,它会变成红色。但是,我想为页面上的所有框添加相同的效果。我不知道该怎么做 - 我试图向整个页面添加一个事件侦听器并使用 target.nodeName 和 target.NodeValue,但无济于事。我已经包含了修复框在鼠标悬停时变为红色的工作版本。
var n=16; //take grid column value as you want
const bigContainer = document.querySelector('.bigContainer')
for(var i = 1; i < n; i++) {
bigContainer.innerHTML+='<div class="row">';
for(j = 0; j < n; j++) {
bigContainer.innerHTML+='<div class="smallBox">';
}
}
const smallBox = document.querySelector('.smallBox');
smallBox.addEventListener('mouseover', () => {
smallBox.classList.add('permahover');
});
.smallBox {
border: 1px solid black;
width: 20px;
height: 20px;
display: inline-block;
}
.permahover {
background: red;
}
h1 {
text-align: center;
}
.bigContainer {
text-align: center;
}
<h1>Etch-a-Sketch Assignment - The Odin Project</h1>
<div class="bigContainer">
</div>
【问题讨论】:
-
@StephenP 不是 OP,但我想 OP 希望悬停状态保持不变,因为 html 的标题是“Etch-a-Sketch Assignment”
标签: javascript html css