html
<ul id = "user-list">
<li>张三</li>
<li>李四</li>
<li>王五</li>
<li>赵六</li>
</ul>
js
let list = document.querySelector("#user-list")
let items = document.querySelectorAll("#user-list > li")
list.addEventListener('click', function(e) {
if(e.target.tagName === "LI") { //请问这2行怎么理解?
let item = e.target
items.forEach(function(item) {
item.style.background = "inherit"
item.style.color = "inherit"
})
item.style.background = "black"
item.style.color = "white"
console.log(item.innerHTML)
}
})
相关分类