一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

基于JQuery的Tab选项卡(实用例子)

时间:2016-03-27 编辑:简简单单 来源:一聚教程网


1.引入文件

css文件

@charset "utf-8";
/* 通用 */
body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, button, textarea, p, blockquote, th, td {margin: 0; padding: 0;}
fieldset, img {border: 0;}
abbr, acronym {border: 0; font-variant: normal;}
body {line-height: 1.5;}
ol, ul {list-style: none;}
table {border-collapse: collapse; border-spacing: 0;}
caption, th {text-align: left;}
sup, sub {font-size: 100%; vertical-align: baseline;}
blockquote, q {quotes: none;}
blockquote:before, blockquote:after, q:before, q:after {content: ''; content: none;}
:link, :visited {text-decoration: none;}
:focus {outline: 0;}
body{ font-family: 'Microsoft YaHei'; background:#ccc;}

/*tab*/
.tab{ margin:10px auto;}
.tab .tab_menu{width:100%; border-bottom:#999 1px dotted;}
.tab .tab_menu ul li{float:left; background:#353744; line- font-size:14px; color:#fff; text-align:center; margin-right:3px; cursor:pointer;}
.tab .tab_menu li.selected {color:#ffea00; transition: all 0.3s ease 0s}
.tab .tab_box{font-size:12px; line- margin-top:1px; background:#eee; padding:5px 10px;}
.tab .tab_box .hidden{display:none;}

2.添加选项卡JS

$(function(){
     var $div_li =$("div.tab_menu ul li");
     $div_li.hover(function(){
   $(this).addClass("selected")   
       .siblings().removeClass("selected");
            var index =  $div_li.index(this); 
   $("div.tab_box > div")  
     .eq(index).show()  
     .siblings().hide();
  })
})

3.Tab结构



 

   

         
  • 1

  •      
  • 2

  •    

 

 

   
1...

   
 

提示:

1.tab.js中$div_li.hover为鼠标经过事件,可更为click为鼠标点击事件
2.$(“div.tab_box > div”) 选取子节点,不选取子节点的话,如果里面还有div,会引起错误。
3.tab_menu li数量要与.tab_box > div数量对应。

热门栏目