导航栏选择

    科技2022-08-18  112

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> *{ margin:0; padding: 0; } #nav{ width: 100%; height: 30px; background-color:yellow; } #nav ul li { float: left; list-style: none; margin-left: auto; border: 2px solid white; width: 80px; height: 30px; text-align: center; line-height: 30px; } .active{ background-color: aqua; } </style> </head> <body> <div id="nav"> <ul> <li>年后</li> <li>dad</li> <li>沙发</li> <li>格式</li> <li>大大</li> </ul> </div> <script type="text/javascript"> var act = document.getElementsByTagName("li"); Array.from(act).forEach(function(sp){//给每个li元素注册点击事件 //Array.from 将伪数组转化为标准数组 //伪数组 伪数组就是像数组的对象,就是一个对象,将其转化为一个数组 //classList:获取类样式 sp.onclick = function() { var aa =document.querySelector("li.active") if(aa) { aa.classList.remove("active") } this.classList.add("active"); } }) </script> </body> </html>

    Processed: 0.067, SQL: 9