JS实现多个Div的切换显示
经常会在网页中看到有个便签里有多个按钮,每个按钮都能显示不同的内容,其实这就是多个Div放在一起然后轮流显示,下面是代码,直接查看代码效果的,可以戳这里www.rain1024.com-js76
下面是代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <script> // 设置每秒刷新一次 // function myshuaxing() { // window.location.reload(); // } // setTimeout('myshuaxing()',2000); </script> <style> .all{ width: 306px; height: 300px; background-color: #616378; border-width: 2px; border-style: solid; border-color: #8C8EA3; } .menu1{ width: 100px; height: 25px; background-color: #CCCCD4; padding-top: 10px; text-align: center; margin-right: 1px; float: left; } .menu2{ width: 100px; height: 25px; background-color: #FFFFFF; padding-top: 10px; text-align: center; margin-right: 2px; margin-left: 2px; float: left; } .menu3{ width: 100px; height: 25px; background-color: #FFFFFF; padding-top: 10px; text-align: center; margin-left: 1px; float: left; } .content1{ width: 306px; height: 200px; padding-top: 100px; text-align: center; background-color: #CCCCD4; } .content2{ width: 306px; height: 200px; padding-top: 100px; text-align: center; background-color: #CCCCD4; display: none; } .content3{ width: 306px; height: 200px; padding-top: 100px; text-align: center; background-color: #CCCCD4; display: none; } </style> <script> function display(value){ if(value==1){ document.getElementById('menu1').style.backgroundColor='#CCCCD4'; document.getElementById('menu2').style.backgroundColor='#FFFFFF'; document.getElementById('menu3').style.backgroundColor='#FFFFFF'; document.getElementById('content1').style.display='block'; document.getElementById('content2').style.display='none'; document.getElementById('content3').style.display='none'; }else if(value==2){ document.getElementById('menu1').style.backgroundColor='#FFFFFF'; document.getElementById('menu2').style.backgroundColor='#CCCCD4'; document.getElementById('menu3').style.backgroundColor='#FFFFFF'; document.getElementById('content1').style.display='none'; document.getElementById('content2').style.display='block'; document.getElementById('content3').style.display='none'; }else if(value==3){ document.getElementById('menu1').style.backgroundColor='#FFFFFF'; document.getElementById('menu2').style.backgroundColor='#FFFFFF'; document.getElementById('menu3').style.backgroundColor='#CCCCD4'; document.getElementById('content1').style.display='none'; document.getElementById('content2').style.display='none'; document.getElementById('content3').style.display='block'; } } </script> <body> <div class="all"> <div id="menu1" class="menu1" onclick="display(1)">tab1</div> <div id="menu2" class="menu2" onclick="display(2)">tab2</div> <div id="menu3" class="menu3" onclick="display(3)">tab3</div> <div id="content1" class="content1"><h1>123</h1></div> <div id="content2" class="content2"><h1>456</h1></div> <div id="content3" class="content3"><h1>789</h1></div> </div> </body> </html> |