本文實例為大家分享了js選項卡效果的具體代碼,供大家參考,具體內容如下
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title></title> <style> *{ margin:0; padding:0; } .box{ margin:50px; } .box div{ display:none; width: 200px; height: 200px; border:1px solid black; } .onclick{ background:red; } input{ border:1px solid #666; padding:2px; } </style> </head> <body> <div class='box'> <input type="button" value='按鈕1' class='onclick'/> <input type="button" value='按鈕2' /> <input type="button" value='按鈕3' /> <div style="display:block">1</div> <div>2</div> <div>3</div> </div> </body> <script> var ipt=document.getElementsByTagName('input'); //獲取所有的按鈕標簽 var oDiv=document.getElementsByClassName('box')[0]; //獲取box標簽 var aDiv=oDiv.getElementsByTagName('div'); //獲取box下面的所有div標簽 for(var i=0;i<ipt.length;i++){ //使用for循環 遍歷所有的input按鈕 ipt[i].index=i; //定義索引為i; ipt[i].onclick=function(){ //點擊事件 for(var j=0;j<ipt.length;j++){ //清除所有的樣式 aDiv[j].style.display='none'; ipt[j].className=''; } //設置adiv的樣式和input的背景樣式 aDiv[this.index].style.display='block'; ipt[this.index].className='onclick'; } } </script></html>補充JQ的做法:
$(function(){ $('.box').find('input').click(function(){ $('.box').find('input').attr('class',''); $(this).attr('class','onclick'); $('.box').find('div').css('display','none'); $('.box').find('div').eq($(this).index()).css('display','block'); }); });以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答