<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> *{ margin: 0; padding:0; } ul{ list-style: none; } #newsBox{ box-sizing: border-box; margin:20px auto; padding: 20px; width:300px; border: 2px solid lightblue; } #newsBox li{ line-height: 5opx; border-bottom:1px dashed lightcoral; } #newsBox li:nth-last-child(1) { border-bottom:none; } /* 基于css实现 */ /* #newsBox li:nth-child(even) { background:lightgray; } #newsBox li:hover{ background: lightgreen; } */ </style></head><body> <ul id="newsBox"> <li>我是第1个LI</li> <li>我是第2个LI</li> <li>我是第3个LI</li> <li>我是第4个LI</li> <li>我是第5个LI</li> </ul> <script> // 首先获取元素 let newsBox = document.getElementById('newsBox'); let newsList = newsBox.getElementsByTagName('li'); // 循环实现 for(let i = 0; i < newsList.length; i++) { let curLi = newsList[i]; curLi.style.background = i % 2 === 0 ? '#fff' : '#ddd'; } curLi.onmouseover = function () { curLi.style.background = 'lightblue' } cruli.onmouseout = function () { curLi.style.background = i % 2 === 0 ? '#fff' : '#ddd'; } </script></body></html>