| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- *{
- margin: 0;
- padding: 0;
- }
- li{
- list-style: none;
- }
- .box{
- width: 600px;
- height: 100px;
- background-color: #999;
- margin:100px auto;
- }
- .box ul{
- display: flex;
- }
- .box li{
- width: 200px;
- height: 100px;
- font-size: 50px;
- font-weight: bold;
- text-align: center;
- line-height: 100px;
- color: white;
- }
- .box ul .active{
- background-color: #111;
- }
- </style>
- </head>
- <body>
- <div class="box">
- <ul>
- <li class="active">first</li>
- <li>second</li>
- <li>thrid</li>
- </ul>
- </div>
- <script>
- var lis = document.querySelectorAll("li");
- // 循环绑定事件
- for(var i=0;i<lis.length;i++){
- lis[i].onclick = function(){
- for(var j=0;j<lis.length;j++){
- lis[j].classList.remove("active");
- }
- this.classList.add("active");
- }
- }
- </script>
- </body>
- </html>
|