2012-01-18 92 views
2

我有这个jQuery方法的两个问题:多个选项卡在一个页面使用jQuery的Css

1-在CSS悬停和活动不会改变链接的颜色! GallerySubmit点击后

2 - 在TAB3或TAB4(ID 3- ID 4)隐藏tabcontent为ID 1.

<div><ul class="tabs"> 
    <li><a href="#tab1">Gallery</a></li> 
    <li><a href="#tab2">Submit</a></li> 
    </ul> 

    <div class="tab_container"> 
     <div id="tab1" class="tab_content"> 
      <!--Content--> 
     </div> 
     <div id="tab2" class="tab_content"> 
      <!--Content--> 
     </div> 
    </div></div> 
<div> 
    <ul class="tabs"> 
     <li><a href="#tab3">Gallery</a></li> 
     <li><a href="#tab4">Submit</a></li> 
    </ul> 

    <div class="tab_container"> 
     <div id="tab3" class="tab_content"> 
      <!--Content--> 
     </div> 
     <div id="tab4" class="tab_content"> 
      <!--Content--> 
     </div> 
    </div> 
</div> 

$(document).ready(function() { 

    //When page loads... 
    $(".tab_content").hide(); //Hide all content 
    $("ul.tabs li:first").addClass("active").show(); //Activate first tab 
    $(".tab_content:first").show(); //Show first tab content 
    //On Click Event 
    $("ul.tabs li").click(function() { 

     $("ul.tabs li").removeClass("active"); //Remove any "active" class 
     $(this).addClass("active"); //Add "active" class to selected tab 
     $(".tab_content").hide(); //Hide all tab content 
     var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content 
     $(activeTab).fadeIn(); //Fade in the active ID content 
     return false; 
    }); 

}); 

ul.tabs { 
    margin: 0; 
    padding: 0; 
    float: left; 
    list-style: none; 
    height: 32px; /*--Set height of tabs--*/ 
    border-bottom: 1px solid #999; 
    border-left: 1px solid #999; 
    width: 100%; 
} 
ul.tabs li { 
    float: left; 
    margin: 0; 
    padding: 0; 
    height: 31px; /*--Subtract 1px from the height of the unordered list--*/ 
    line-height: 31px; /*--Vertically aligns the text within the tab--*/ 
    border: 1px solid #999; 
    border-left: none; 
    margin-bottom: -1px; /*--Pull the list item down 1px--*/ 
    overflow: hidden; 
    position: relative; 
    background: #e0e0e0; 
} 
ul.tabs li a { 
    text-decoration: none; 
    color: #000; 
    display: block; 
    font-size: 1.2em; 
    padding: 0 20px; 
    border: 1px solid #fff; /*--Gives the bevel look with a 1px white border inside the list item--*/ 
    outline: none; 
} 
ul.tabs li a:hover { 
    background: #ccc; 
} 
html ul.tabs li.active, html ul.tabs li.active a:hover { /*--Makes sure that the active tab does not listen to the hover properties--*/ 
    background: #fff; 
    border-bottom: 1px solid #fff; /*--Makes the active tab look like it's connected with its content--*/ 
} 
.tab_container { 
    border: 1px solid #999; 
    border-top: none; 
    overflow: hidden; 
    clear: both; 
    float: left; width: 100%; 
    background: #fff; 
} 
.tab_content { 
    padding: 20px; 
    font-size: 1.2em; 
} 

如何解决这个问题! LIVE Action Demo

+1

为什么你使用jQuery和jQueryui的旧版本? – nobody 2012-01-18 16:15:20

+0

JqueryUI =高分辨率 – 2012-01-18 16:31:08

+0

您可以构建自定义下载。 http://jqueryui.com/download – nobody 2012-01-18 17:24:44

回答

3

按类隐藏元素。无论选项卡设置如何,$(".tab_content").hide()都会隐藏您的所有内容元素。

你应该包装你的标签和它的内容区域,并选择相对于该包装元素的元素。

UPDATE

为您推荐的短demo

$(".myTabs").each(function() { 

    var $myTabs = $(this); 

    $myTabs.find(".tab_content").hide().first().show(); 
    $myTabs.find("ul.tabs li:first").addClass("active").show(); 

    $myTabs.find("ul.tabs li").click(function() { 
     var $this = $(this); 

     $this.addClass("active").siblings().removeClass("active"); 
     $myTabs.find(".tab_content").hide(); 

     var activeTab = $this.find("a").attr("href"); 
     $(activeTab).fadeIn(); 

     return false; 
    }); 
}); 
+0

这不行!点击提交后不删除图库内容!将内容放入图库/提交!看到这个错误 – 2012-01-18 16:42:03

+0

什么错误?你设法解决它吗? – Stefan 2012-01-19 08:14:57

0

正确答案是这样的:

$(document).ready(function() { 

$(".myTabs").each(function() { 

    var $myTabs = $(this); 

    $myTabs.find(".tab_content").hide().first().show(); 
    $myTabs.find("ul.tabs li:first").addClass("active").show(); 

    $myTabs.find("ul.tabs li").click(function() { 
     var $this = $(this); 

     $this.addClass("active").siblings().removeClass("active"); 
     $myTabs.find(".tab_content").hide(); 

     var activeTab = $this.find("a").attr("href"); 
     $(activeTab).fadeIn(); 

     return false; 
    }); 
}); 

});

相关问题