tab标签切换

tab标签切换经常用到,所以写了一个简单的demo,支持ie6+浏览器。

html代码

<ul class="tab clearfix">
    <li>标签1</li>
    <li class="tab-active">标签2</li>
    <li>标签3</li>
</ul>
<div class="content">
    <div class="inner" style="display:none">
        标签1内容
    </div>
    <div class="inner">
        标签2内容
    </div>
    <div class="inner" style="display:none">
        标签3内容
    </div>
</div>   

css

.content{
    border:1px solid #ddd;
    width:730px;
    height: 200px;
    background: #fff;
    *position: relative;
    *z-index: 10;
}
.tab{
    margin-top: 12px;
    width: 730px;
    height: 30px;
    list-style: none;
    margin-bottom: -1px;
    *position: relative;
    *z-index: 20;   
}
li{
    float: left;
    height: 28px;
    padding: 0 25px;
    border: 1px solid #DDDDDD;
    margin-right: 10px;
    line-height: 30px;
    cursor: pointer;
    background: #FAFAFA;
    color: #0461B1;
    font-size: 14px;
}
.tab-active{
    border-bottom:none;
    border-top-width: 2px;
    height: 28px;
    background: #fff;
    color: #333333;
    font-weight: bold;
}

js

 1 <script type="text/javascript">
 2     var lists = $('.tab li');
 3     var contents = $('.content .inner');
 4 
 5     function bindEvent(){
 6 
 7         lists.each(function(index_li, li){
 8             $(this).on('click', function(event){
 9 
10                 lists.removeClass('tab-active');
11                 $(this).addClass('tab-active');
12 
13                 contents.each(function(index_content, content){
14                     if(index_li === index_content){
15                         $(this).show();
16                     }else{
17                         $(this).hide();
18                     }
19                 });
20             });
21         });
22     }
23 
24     function init(){
25         bindEvent();
26     }
27 
28     init();
29 </script>

效果

posted @ 2013-07-02 15:47  zhaoran  阅读(13258)  评论(8编辑  收藏  举报