<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>隔行变色</title> <script> window.onload = function () { var aLi = document.getElementsByTagName('li'); for (var i = 0; i < aLi.length; i++) { if (i % 2 == 0) { aLi[i].style.background = '#333'; }else{ aLi[i].style.background='red'; } } } </script> <body> <ul> <li>2</li> <li>2</li> <li>2</li> <li>2</li> <li>2</li> <li>1</li> </ul> </body> </html>