给当前选中分类加背景色
Posted 贺小鸣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了给当前选中分类加背景色相关的知识,希望对你有一定的参考价值。
<!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>
效果图:
以上是关于给当前选中分类加背景色的主要内容,如果未能解决你的问题,请参考以下文章