1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <!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;
- }
- li {
- width: 50px;
- height: 30px;
- border: 1px solid #ccc;
- text-align: center;
- line-height: 30px;
- border-radius: 10px;
- background: aqua;
- float: left;
- }
- #div1 {
- width: 400px;
- height: 200px;
- border: 1px solid #ccc;
- }
- .selected {
- color: white;
- background: orange;
- }
- .active {
- display: none;
- }
- .select {
- 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="active select">时事内容</div>
- <div class="active">新闻内容</div>
- <div class="active">体育内容</div>
- </div>
- </div>
- <script>
- var btns = document.getElementsByTagName('li')
- var content = document.getElementsByClassName('active')
- for (var i = 0; i < btns.length; i++) {
- btns[i].index = i
- console.log(btns[i].index)
- btns[i].onclick = function () {
- for (var j = 0; j < btns.length; j++) {
- btns[j].className = ' '
- content[j].className = 'active'
- }
- // console.log(this.index)
- //谁的点击事件就是谁 btn[i].onclick btns[]
- this.className = 'selected'
- //content[索引值] = content[btns点击谁的索引值] =content[this.index]
- content[this.index].className = 'active select'
- }
- }
- </script>
- </body>
- </html>
|