挑出来。感谢所有帮助我的人,当 id 属性丢失时(或者由于某种原因不允许调用单个元素时),如何在页面上的多个 DIV 元素上调用 JavaScript 代码。
我希望能够通过更改背景颜色(内容)和h 标签的颜色(单击的位置)来对每个元素执行某些操作,但我不想单独访问每个元素。
Object.entries('.Container').map(( object ) => {
object[1].addEventListener("click", function() {
// Output innerHTML of the clicked element
console.log("Hello " + this + " (" + this.innerHTML + ") from map method...");
});
});
body {
background-color: rgba(255, 255, 255, 1);
margin: 0px; padding: 0px;
height: 100vh; width: 100%;
display: flex; flex-direction: column;
align-items: center; justify-content: center;
overflow: hidden;
}
.Container {
background-color: lightgray;
margin: 0; padding: 0;
height: auto; width: 250px;
display: flex; flex-direction: column;
align-items: center; justify-content: center;
overflow: hidden;
}
.content {
background-color: lightcyan;
margin: 5px; padding: 0;
height: auto; width: 80%;
display: flex; flex-direction: row;
align-items: center; justify-content: center;
overflow: hidden;
}
h3 {
color: red;
}
<div class="Container">
<div class ="content"><h3>content 1</h3></div>
<div class ="content"><h3>content 2</h3></div>
<div class ="content"><h3>content 3</h3></div>
</div>
繁星coding
慕仙森
相关分类