最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery Simple Tab Navigation - Stack Overflow

programmeradmin0浏览0评论

I want a simple navigation without using any plugin but simple JQuery.

Link 1 Div 1,Div 2,Div 3,Div 4
Link 2
Link 3
Link 4

When user clicks Link 1 show Div 1 and hide all other divs. Link 2 then show Div2 and hide all others. I want to do this with less number of lines.

I want a simple navigation without using any plugin but simple JQuery.

Link 1 Div 1,Div 2,Div 3,Div 4
Link 2
Link 3
Link 4

When user clicks Link 1 show Div 1 and hide all other divs. Link 2 then show Div2 and hide all others. I want to do this with less number of lines.

Share Improve this question edited May 30, 2017 at 12:53 Talha Awan 4,6194 gold badges26 silver badges40 bronze badges asked Apr 1, 2011 at 16:35 Pit DiggerPit Digger 9,80023 gold badges81 silver badges126 bronze badges 1
  • 3 how about jquery ui? ui can do that with $(".tabs").tabs(); – corroded Commented Apr 1, 2011 at 16:38
Add a ment  | 

6 Answers 6

Reset to default 2

Here is a snippet I use all the time (original): JS:

    $(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;
    });

});

css (a lot of improvement area here hehe):

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;
}

markup:

    <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">
        asdfasdfasdfasdf
    </div>
    <div id="tab2" class="tab_content">
        asdfasdf
    </div>
</div>

Hope it helps

I've made one for you at jsfiddle, hope this help. cheers

https://jqueryui./tabs/ is a plugin but it also counts as simple jQuery. Have you considered that option?

Any reason why you don't want to use a plugin? Here is a great plugin that will do everything you are asking: http://flowplayer/tools/tabs/index.html

here is a simple script, just add in your file and it will work, also no conflict with other existing scripts.

See demo: http://simple-jquery-responsive-tab.blogspot.in/

You can check this ponent on jsFiddle.

It can also be used if you want to populate dynamically.

$(document).ready(function() {
$.each($('.tabwrapper .tabmenu ul.nav li'), function(i) {
    $(this).attr('data-tab', i);
});
$.each($('.tabwrapper .tabcontent .tabs'), function(i) {
    $(this).attr('data-tab', i);
});
$('.tabwrapper .tabmenu ul.nav li a').click(function() {
    var parent = $(this).parent(),
        dataId = parent.data('tab');
    if (!parent.hasClass('active')) {
        $('.tabwrapper .tabmenu ul.nav li').removeClass('active');
        parent.addClass('active');
        $('.tabwrapper .tabcontent .tabs').hide();
        $('.tabwrapper .tabcontent .tabs[data-tab="' + dataId + '"]').fadeIn();
    }
});

});

发布评论

评论列表(0)

  1. 暂无评论