jQuery 内容 Controller 没有正确显示和隐藏内容

标签 jquery html css

过去几天我一直在尝试制作一个导航栏,但我似乎遇到了很多问题。现在我的问题是 jQuery,它没有按照我想要的方式运行:

我想要一个单页网站,所以每当我单击导航栏时,当前页面应该隐藏,目标页面应该显示。现在,有些页面仍然堆叠在一起,有人可以帮我吗?

HTML代码

<!DOCTYPE html>
<html> 
    <head>
        <title>Yu-Gi-Oh! Stash</title>
        <link rel="stylesheet" type="text/css" href="styles/style.css">
        <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
        <script src="scripts/contentcontroller.js"></script>
        <script src="scripts/navbar.js"></script>
        <meta charset="utf-8">
    </head>

    <body>        
        <section> 
            <nav>
                <ul>
                    <li class="active"><a href="#decks">Decks &blacktriangledown;</a>
                        <ul>
                            <li><a href="#decks_starter-decks">Starter Decks</a></li>
                            <li><a href="#decks_structure-decks">Structure Decks</a></li>
                        </ul>
                    </li>

                    <li><a href="#booster-packs">Booster Packs &blacktriangledown;</a>
                        <ul>
                            <li><a href="#booster-packs_booster-sets">Booster Sets</a></li>
                            <li><a href="#booster-packs_special-edition">Special Editions</a></li>
                            <li><a href="#booster-packs_duelist-packs">Duelist Packs</a></li>
                            <li><a href="#booster-packs_master-collections">Master Collections</a></li>
                        </ul>
                    </li>

                    <li><a href="#torunament-awards">Tournament Awards</a></li>

                    <li><a href="#promotions">Promotions &blacktriangledown;</a>
                        <ul>
                            <li><a href="#promotions_video-games">Video Games</a></li>
                            <li><a href="#promotions_entertainments">Entertainment</a></li>
                            <li><a href="#promotions_foundations">Foundations</a></li>
                        </ul>
                    </li>
                </ul>
            </nav>

            <section id="decks" class="tab-content active">
                <h1 class="page-heading">Decks</h1>

                <p>lorem ipsum..</p>
            </section>

            <section id="decks_starter-decks" class="tab-content hide">
                <h1 class="page-heading">Starter Decks</h1>

                <p>lorem ipsum..</p>
            </section>

            <section id="decks_structure-decks" class="tab-content hide">
                <h1 class="page-heading">Structure Decks</h1>

                <p>lorem ipsum..</p>
            </section>

            <section id="booster-packs" class="tab-content hide">
                <h1 class="page-heading">Booster Packs</h1>

                <p>lorem ipsum..</p>
            </section>

            <section id="booster-packs_booster-sets" class="tab-content hide">
                <h1 class="page-heading">Booster Sets</h1>

                <p>lorem ipsum..</p>
            </section>

            <section id="booster-packs_special-edition" class="tab-content hide">
                <h1 class="page-heading">Special Edition</h1>

                <p>lorem ipsum..</p>
            </section>

            <section id="booster-packs_duelist-packs" class="tab-content hide">
                <h1 class="page-heading">Duelist Packs</h1>

                <p>lorem ipsum..</p>
            </section>

            <section id="booster-packs_master-collections" class="tab-content hide">
                <h1 class="page-heading">Master Collections</h1>

                <p>lorem ipsum..</p>
            </section>

            <section id="torunament-awards" class="tab-content hide">
                <h1 class="page-heading">Tournament Awards</h1>

                <p>lorem ipsum..</p>
            </section>

            <section id="promotions" class="tab-content hide">
                <h1 class="page-heading">Promotions</h1>

                <p>lorem ipsum..</p>
            </section>

            <section id="promotions_video-games" class="tab-content hide">
                <h1 class="page-heading">Video Game Promotions</h1>

                <p>lorem ipsum..</p>
            </section>

            <section id="promotions_entertainments" class="tab-content hide">
                <h1 class="page-heading">Magazine, Movie & McDonalds Promotions</h1>

                <p>lorem ipsum..</p>
            </section>

            <section id="promotions_foundations" class="tab-content hide">
                <h1 class="page-heading">Foundations</h1>

                <p>lorem ipsum..</p>
            </section>
        </section>

        <footer>
        </footer>
    </body>
</html>

CSS代码

body, html {
    padding: 0;
    margin: 0;
    font-family: "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif;
}

p {
    padding: 0;
    margin: 35px 25px 25px 25px;
}

h1, h2, h3, h4, h5, h6 {
    margin-top: 0;
    margin-bottom: 0;
    text-transform: capitalize;
}

section {
    padding: 0;
    margin: auto;
    width: 85%;
    height: 1000px;
    border-top: 1px solid #fff;
}

section section {
    border: none;
}

.page-heading {
    margin: 80px 0px 80px 0px;
    text-align: center;
    font-weight: 700;
    font-size: 25px;
    line-height: 32px;
    color: #06b;
}

/********************
 ***NAVIGATION BAR***
 ********************/
nav {
    position: fixed;
    width: inherit;
    margin: -1px 0px 0px -1px;
    list-style-type: none !important;
    background-color: #fff;
    border: 1px solid #aaa;
    border-top: none;

    -webkit-border-bottom-left-radius: 5px;
    -moz-border-radius-bottomleft: 5px;
    border-bottom-left-radius: 5px;
    -webkit-border-bottom-right-radius: 5px;
    -moz-border-radius-bottomright: 5px;
    border-bottom-right-radius: 5px;
}

nav:after {
    clear: both;
}

nav ul {
    list-style: none;
    position: relative;
    float: left;
    margin: 0;
    padding: 8px;
    padding-bottom: 0;
}

nav ul li {
    width: auto;
    position: relative;
    float: left;
    margin: 0 25px -1px 0;
    padding: 0;
}

nav ul li.active > a {
    color: #06b;
    border-color: #aaa #aaa #fff;
}

nav ul li:hover > a {
    background: #fff;
    border-color: #aaa #aaa #fff;
}

nav ul a:link, a:visited {
    display: block;
    color: #09c;
    text-decoration: none;
    text-transform: capitalize;
    font-weight: 600;
    font-size: 15px;
    line-height: 32px;
    margin: 0;
    padding: 0 15px;
    border: 1px solid;
    border-color: #eee #eee transparent;

    -webkit-border-top-left-radius: 5px;
    -webkit-border-top-right-radius: 5px;
    -moz-border-radius-topleft: 5px;
    -moz-border-radius-topright: 5px;
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
}

nav ul ul {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    background: #fff;
    padding: 0;
}

nav ul ul li {
    float: none;
    width: 150%;
    margin: 0;
    background-color: #fff;
}

nav ul ul a {
    line-height: 120%;
    padding: 10px 15px;
    border-top: none;
    border-color: #eee !important;

    -webkit-border-radius: 0px !important;
    -moz-border-radius: 0px !important;
    border-radius: 0px !important;
}

nav ul ul a:hover {
    border-color: #aaa !important;
}

nav ul ul ul {
    top: 0;
    left: 100%;
}

nav ul li:hover > ul {
    display: block;
}

/*************
 ***CONTENT***
 *************/
.tab-content.active {
    display: block;
}

.tab-content.hide {
    display: none;
}

jQuery 代码

/***************************
 ***Navbar click function***
 ***************************/
$(document).ready(function() {
    $('nav li > a').click(function(event) {
        event.preventDefault();

        //declare current tab content
        var current_tab_content = $('nav li.active > a').attr('href');

        //hide current tab content
        $(current_tab_content).removeClass('active').addClass('hide');

        //show targeted tab content
        var targeted_tab_content = $(this).attr('href');
        $(targeted_tab_content).removeClass('hide').addClass('active');

        //remove 'active' from current navbar
        var current_navbar = $('nav li.active');
        $(current_navbar).removeClass('active');

        //add 'active' to clicked navbar
        $(this).parents('li').last().addClass('active');
    });
});

最佳答案

点击导航栏几次后,您似乎最终看到了不止一个类别为“事件”的部分。

尝试先隐藏所有部分,然后显示所需的部分,而不是一次隐藏一个选项卡:

$('.tab-content').removeClass('active').addClass('hide');
$(targeted_tab_content).addClass('active').removeClass('hide');

其他建议

  1. 您可能只有一个类而不是两个类,将所有内容默认为 display: none; 然后您只需删除/分配“事件”类。

  2. show()hide() 还有一个 jQuery 函数,在这里可能很有用。

关于jQuery 内容 Controller 没有正确显示和隐藏内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31014611/

相关文章:

jquery - 未捕获的语法错误 : Unexpected token function

html - @media 查询不适用于主样式表,将其链接为外部工作表

css - 我如何将悬停属性与两个类一起使用

javascript - 如何在 url 中制作一盒 nxn?

css - vim 中凌乱的 css 缩进

javascript - 在 Vue.js 中有条件地定位具有 CSS 样式的类

javascript - Slick carousel - 强制幻灯片具有相同的高度

jquery - 除了first和second怎么选LI?

javascript - 获取窗口 Javascript 的正确尺寸

javascript - html表格中输入类型文本名称