1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- *{
- margin: 0;
- padding: 0;
- }
- ul{
- list-style: none;
- }
- #btn{
- overflow: hidden;
- }
- #btn li {
- width: 50px;
- height: 30px;
- background: aquamarine;
- text-align: center;
- line-height: 30px;
- border: 1px solid #CCC;
- border-radius: 10px;
- float: left;
- }
- #btn .selected{
- background: orange;
- color: white;
- }
- #div1{
- width: 350px;
- height: 200px;
- border: 1px solid #ccc;
- }
- .actived{
- display: none;
- }
- .choice{
- display: block;
- }
- </style>
- </head>
- <body>
- <div id="container">
- <ul id="btn">
- <li class="selected">时事</li>
- <li>新闻</li>
- <li>体育</li>
- </ul>
- <div id="div1">
- <div class="actived choice">时事内容</div>
- <div class="actived">新闻内容</div>
- <div class="actived">体育内容</div>
- </div>
- </div>
- <script>
- var btn = document.getElementById('btn')
- var btns = btn.getElementsByTagName('li')
- var content = document.getElementsByClassName('actived')
- console.log(content)
- for(var i=0;i<btns.length;i++){
- btns[i].index = i
- btns[i].onclick = function(){
- //this 点谁就是谁
- for(var j=0;j<btns.length;j++){
- btns[j].className = ''
- content[j].className = 'actived'
- }
- this.className = 'selected'
- console.log(this.index)
- content[this.index].className = 'actived choice'
- }
- }
-
- </script>
- </body>
- </html>
|