使用for of 循环操作元素:
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<script>
let lis = document.querySelectorAll('li');
for (let val of lis) {
val.addEventListener('click', function() {
val.innerText = +val.innerText + 1
val.style.backgroundColor = 'red'
})
};
</script>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
<script>
let lis = document.querySelectorAll('li');
for (let vals of lis) {
vals.addEventListener('click', function() {
vals.style.backgroundColor = 'blue'
})
};
</script>