1. 程式人生 > 程式設計 >js實現選項卡效果

js實現選項卡效果

本文例項為大家分享了js實現選項卡效果的具體程式碼,供大家參考,具體內容如下

<!DOCTYPE html>
<html>
<head>
 <title></title>
</head>
<script type="text/javascript">
 window.onload=function(){
 var ob = document.getElementById('div1');
 var ob1 = div1.getElementsByTagName('input');
 var ob2= div1.getElementsByTagName('div');
 
 for(var i = 0;i< ob1.length;i++){
  ob1[i].index=i;
  ob1[i].onmouseover=function(){
  
  for(var i = 0;i< ob1.length;i++){
  ob2[i].style.display='none';
   ob1[i].className='';
      } 
 this.className='active';
  ob2[this.index].style.display='block';

 
 } 
  ob1[i].onmouseout=function(){
  for(var i=0 ; i< ob1.length;i++){
  ob2[i]['style']['display']='none';
  }
  }

 } 

 }; 
 
</script>
<body>
<style type="text/css">
#div1 div{
 width: 100px;
 height: 100px;
 border-top: 2px solid grey;
 background-color: #ccc;
} 
.active {
 background-color: yellow;
}

</style>
<div id="div1">
 
<input type="button" value="1" class="active">
<input type="button" value="2">
<input type="button" value="3">
<input type="button" value="4">

<div style="display:block;" >1111</div>
<div style="display: none;">2222</div>
<div style="display: none;">3333</div>
<div style="display: none;">4444</div>

</div>

</body>
</html>

如果大家還想深入學習,可以點選兩個精彩的專題:javascript選項卡操作方法彙總 jquery選項卡操作方法彙總

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。