给当前选中分类加背景色
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>给当前选中的分类加背景色</title> <style> li{cursor:pointer;float: left;width: 25%;text-align: center;list-style: none} .cur{background:red;} </style> </head> <body> <ul> <li class="cur"><a href="javascript:void(0)">1</a></li> <li><a href="javascript:void(0)">2</a></li> <li><a href="javascript:void(0)">3</a></li> <li><a href="javascript:void(0)">4</a></li> </ul> </body> </html> <script> window.onload = function () { var aLi = document.getElementsByTagName("li"); var i = 0; for (i = 0; i < aLi.length; i++) { aLi[i].onclick = function () { for (i = 0; i < aLi.length; i++) aLi[i].className = ""; this.className = "cur"; }; } }; </script>
效果图: