原生JS与jQuery编写简单选项卡
本文实例为大家分享了JS编写简单选项卡的具体代码,供大家参考,具体内容如下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>jquery选项卡</title> <style type="text/css"> #div1 div{width: 200px;height: 200px;border: 1px red solid;display: none;} .active{background:#999;} </style> <!-- 原生的JS --> <script type="text/javascript"> window.onload=function(){ var oDiv=document.getElementById('div1'); var aInput=document.getElementsByTagName('input'); var aCon=oDiv.getElementsByTagName('div'); for (var i = 0; i < aInput.length; i++) { aInput[i].index=i; aInput[i].onclick=function(){ for (var i = 0; i < aInput.length; i++) { aInput[i].className=''; aCon[i].style.display=''; } this.className='active'; aCon[this.index].style.display='block'; } } } </script> <!-- jquery写法 --> <script type="text/javascript" src='../jquery-3.2.1.min.js'></script> <script type="text/javascript"> $(function(){ $('#div1').find('input').click(function(){ $('#div1').find('input').attr('class',''); $('#div1').find('div').css('display','none'); $(this).attr('class','active'); $('#div1').find('div').eq($(this).index()).css('display','block'); }) }) </script> </head> <body> <div id="div1"> <input class="active" type="button" value="1"> <input type="button" value="2"> <input type="button" value="3"> <div style="display: block;">11111</div> <div>22222</div> <div>33333</div> </div> </body> </html>
相关推荐
APCDE 2020-06-11
viewerlin 2020-05-25
adc00 2020-01-04
老高 2019-12-30
luoj 2011-09-13
xiaoguolaoshi 2008-07-20
Rgenxiao 2011-09-08
支离破碎 2014-06-30
qiuhaotc 2012-02-19
85931235 2012-03-28
bruceli 2019-05-14
南城小伙 2019-06-10
逆风飞舞0 2019-05-14
87530399 2014-05-26
chenyangah 2017-03-31
84236532 2016-11-17
zhangpuego 2016-07-16
Jiminull 2016-03-03