<style>ul{list-style: none;}</style><body><ul><li>111</li><li>222</li><li>333</li><li>444</li><li>555</li><li>666</li></ul><script>//要操作li 先获取所有livar lis = document.querySelectorAll('li')console.log(lis);// for循环里的 变量 i 要用let 定义 !!!!!!!// 用var 有时会报错 // 1.隔行换色for(let i=0; i<=lis.length-1; i++){if(i%2===0){lis[i].style.backgroundColor = 'pink'}else{lis[i].style.backgroundColor = 'lightblue'}// 2.移上去变色lis[i].onmouseover = function(){// 这里要用 thisthis.style.backgroundColor = 'green'}// 3.移出变为原来的颜色lis[i].onmouseout = function(){if(i%2===0){lis[i].style.backgroundColor = 'pink'}else{lis[i].style.backgroundColor = 'lightblue'}}}</script></body>
100字范文,内容丰富有趣,生活中的好帮手!