javascript - JQuery 简单标签导航

标签 javascript jquery tabs navigation

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

链接 1 Div 1,Div 2,Div 3,Div 4
链接 2
链接 3
链接4

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

最佳答案

这是我一直使用的一个片段 ( 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>

希望对你有帮助

关于javascript - JQuery 简单标签导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5516245/

相关文章:

javascript - 当 child 悬停并单击时设置父级样式

javascript - 创建类似 Twitter 的状态更新小部件的最简单方法

Javascript/jquery 在 mod_rewrite 开启时无法加载

javascript - 将参数传递给回调

jquery - 通过单击事件更改所有元素的样式

jquery - Chrome 扩展 - 查找所有打开的选项卡并在所有选项卡上执行脚本

javascript - 如何创建我们在 google 页面上看到的要求您将 Google 设置为主页的气泡?

javascript - Filtered.push 不是一个函数

javascript - jQuery递归搜索类的父元素

twitter-bootstrap - 平滑滚动和 Bootstrap 选项卡冲突