【问题标题】:JQuery Simple Tab NavigationJQuery 简单选项卡导航
【发布时间】:2011-04-01 16:35:55
【问题描述】:

我想要一个简单的导航,不使用任何插件,只使用简单的 JQuery。

链接 1 第 1 区,第 2 区,第 3 区,第 4 区
链接 2
链接 3
链接4

当用户点击 Link 1 时,显示 Div 1 并隐藏所有其他 div。链接 2 然后显示 Div2 并隐藏所有其他。我想用更少的行数来做到这一点。

【问题讨论】:

  • jquery ui 怎么样? ui 可以使用 $(".tabs").tabs();

标签: javascript jquery tabs navigation


【解决方案1】:

这是我一直使用的 sn-p (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(这里有很多改进的地方呵呵):

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

标记:

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

希望对你有帮助

【讨论】:

    【解决方案2】:

    我在jsfiddle 为您制作了一个,希望对您有所帮助。干杯

    【讨论】:

    • 学到了一些关于 jsfiddle 的新东西。您可以如此轻松地对代码进行示例,这真是太棒了。
    【解决方案3】:

    https://jqueryui.com/tabs/ 是一个插件,但它也算作简单的 jQuery。你考虑过这个选项吗?

    【讨论】:

      【解决方案4】:

      您为什么不想使用插件?这是一个很棒的插件,可以满足您的所有要求:http://flowplayer.org/tools/tabs/index.html

      【讨论】:

        【解决方案5】:

        这是一个简单的脚本,只需添加到你的文件中,它就可以工作,也不会与其他现有脚本冲突。

        查看演示:http://simple-jquery-responsive-tab.blogspot.in/

        【讨论】:

          【解决方案6】:

          你可以查看这个component on jsFiddle

          如果您想动态填充,也可以使用它。

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

          });

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多