javascript - JQuery Simple Tab Navigation - Stack Overflow

I want a simple navigation without using any plugin but simple JQuery.Link 1 Div 1,Div 2,Div 3,

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

});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744062453a4552027.html

相关推荐

  • javascript - JQuery Simple Tab Navigation - Stack Overflow

    I want a simple navigation without using any plugin but simple JQuery.Link 1 Div 1,Div 2,Div 3,

    9天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
能,也可根据模型 hook 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>