首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JQuery简单选项卡导航

JQuery简单选项卡导航
EN

Stack Overflow用户
提问于 2011-04-02 00:35:56
回答 6查看 6.8K关注 0票数 3

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

链接1分部1、分部2、分部3、分部4

链路2

链路3

链路4

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

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-04-02 03:13:44

我在jsfiddle为你做了一个,希望这对你有帮助。干杯

票数 1
EN

Stack Overflow用户

发布于 2011-04-02 02:15:47

下面是我一直使用的代码片段(original):JS:

代码语言:javascript
复制
    $(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 (这里有很多改进的地方):

代码语言:javascript
复制
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;
}

标记:

代码语言:javascript
复制
    <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
EN

Stack Overflow用户

发布于 2019-08-16 06:35:36

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5516245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档