无法使jquery正常工作,我尝试调试但无法正常工作

时间:2020-04-28 06:11:32

标签: jquery flexbox

$(function() {
    $(".toggle").on("click", function() {
        if ($(".item").hasClass("active")) {
            $(".item").removeClass("active");
            $(this).find("a").html("<i class='fas fa-bars'></i>");
        } else {
            $(".item").addClass("active");
            $(this).find("a").html("<i class='fas fa-times'></i>");
        }
    });
});
nav {
    background: #222;
    padding: 5px 20px;
}
ul {
    list-style-type: none;
}
a {
    color: white;
    text-decoration: none;
}
a:hover {
    text-decoration: underline;
}
.logo a:hover {
    text-decoration: none;
}
.menu li {
    font-size: 16px;
    padding: 15px 5px;
    white-space: nowrap;
}
.logo a,
.toggle a {
    font-size: 20px;
}
/* Mobile menu */
.menu {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
}
.toggle {
    order: 1;
}
.item.button {
    order: 2;
}
.item {
    width: 100%;
    text-align: center;
    order: 3;
    display: none;
}
.item.active {
    display: block;
}
/* Tablet menu */
@media all and (min-width: 600px) {
    .menu {
        justify-content: center;
    }
    .logo {
        flex: 1;
    }
    .toggle {
        flex: 1;
        text-align: right;
    }
    .item.button {
        width: auto;
        order: 1;
        display: block;
    }
    .toggle {
        order: 2;
    }
    .button.secondary {
        border: 0;
    }
    .button a {
        padding: 7.5px 15px;
        background: teal;
        border: 1px #006d6d solid;
    }
    .button.secondary a {
        background: transparent;    
    }
    .button a:hover {
        text-decoration: none;
    }
    .button:not(.secondary) a:hover {
        background: #006d6d;
        border-color: #005959;
    }
    .button.secondary a:hover {
        color: #ddd;
    } 
}
/* Desktop menu */
@media all and (min-width: 900px) {
    .item {
        display: block;
        width: auto;
    }
    .toggle {
        display: none;
    }
    .logo {
        order: 0;
    }
    .item {
        order: 1;
    }
    .button {
        order: 2;
    }
    .menu li {
        padding: 15px 10px;
    }
    .menu li.button {
        padding-right: 0;
    }
}
<nav>
    <ul class="menu">
        <li class="logo"><a href="#">Invisible App</a></li>
        <li class="item"><a href="#">Home</a></li>
        <li class="item"><a href="#">About</a></li>
        <li class="item"><a href="#">Services</a></li>
        <li class="item"><a href="#">Features</a></li>
        <li class="item"><a href="#">Blog</a></li>
        <li class="item"><a href="#">Contact</a>
        </li>
        <li class="item button"><a href="#">Log In</a></li>
        <li class="item button secondary"><a href="#">Sign Up</a></li>
        <li class="toggle"><a href="#"><i class="fas fa-bars"></i></a></li>
    </ul>
</nav>
	
<main>
</main>

I have found this awesome tutorial for a hamburger menu using flexbox, but my jquery isn't working. I have tried to move where the jquery is in the document but have had no luck. Does anyone have any ideas why?

编辑 我在下面添加了HTML的CSS。如果我误解了某些内容或错过了一些内容,我还将提供本教程的链接。基本上,当网站缩小为移动设计时,汉堡包图标不起作用,我单击并没有任何反应。抱歉,我应该早些解释。 jQuery中有一个错误,但我不知道如何解决,我是使用javascript和jquery的新手。
谢谢

    <body>

    <nav>
        <ul class="menu">
            <li class="logo"><a href="#">Invisible App</a></li>
            <li class="item"><a href="#">Home</a></li>
            <li class="item"><a href="#">About</a></li>
            <li class="item"><a href="#">Services</a></li>
            <li class="item"><a href="#">Features</a></li>
            <li class="item"><a href="#">Blog</a></li>
            <li class="item"><a href="#">Contact</a>
            </li>
            <li class="item button"><a href="#">Log In</a></li>
            <li class="item button secondary"><a href="#">Sign Up</a></li>
            <li class="toggle"><a href="#"><i class="fas fa-bars"></i></a></li>
        </ul>
    </nav>

    <main>
    </main>
    <script>
    $(function() {
        $(".toggle").on("click", function() {
            if ($(".item").hasClass("active")) {
                $(".item").removeClass("active");
                $(this).find("a").html("<i class='fas fa-bars'></i>");
            } else {
                $(".item").addClass("active");
                $(this).find("a").html("<i class='fas fa-times'></i>");
            }
        });
    });
    </script>
    </body>



nav {
    background: #222;
    padding: 5px 20px;
}
ul {
    list-style-type: none;
}
a {
    color: white;
    text-decoration: none;
}
a:hover {
    text-decoration: underline;
}
.logo a:hover {
    text-decoration: none;
}
.menu li {
    font-size: 16px;
    padding: 15px 5px;
    white-space: nowrap;
}
.logo a,
.toggle a {
    font-size: 20px;
}
/* Mobile menu */
.menu {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
}
.toggle {
    order: 1;
}
.item.button {
    order: 2;
}
.item {
    width: 100%;
    text-align: center;
    order: 3;
    display: none;
}
.item.active {
    display: block;
}

2 个答案:

答案 0 :(得分:1)

您应该在脚本标签之前添加jQuery库的引用...
在脚本标记之前添加此行

<script src="https://code.jquery.com/jquery-3.5.0.min.js"></script>

答案 1 :(得分:1)

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<style>
nav {
    background: #222;
    padding: 5px 20px;
}
ul {
    list-style-type: none;
}
a {
    color: white;
    text-decoration: none;
}
a:hover {
    text-decoration: underline;
}
.logo a:hover {
    text-decoration: none;
}
.menu li {
    font-size: 16px;
    padding: 15px 5px;
    white-space: nowrap;
}
.logo a,
.toggle a {
    font-size: 20px;
}
/* Mobile menu */
.menu {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    align-items: center;
}
.toggle {
    order: 1;
}
.item.button {
    order: 2;
}
.item {
    width: 100%;
    text-align: center;
    order: 3;
    display: none;
}
.item.active {
    display: block;
}
/* Tablet menu */
@media all and (min-width: 600px) {
    .menu {
        justify-content: center;
    }
    .logo {
        flex: 1;
    }
    .toggle {
        flex: 1;
        text-align: right;
    }
    .item.button {
        width: auto;
        order: 1;
        display: block;
    }
    .toggle {
        order: 2;
    }
    .button.secondary {
        border: 0;
    }
    .button a {
        padding: 7.5px 15px;
        background: teal;
        border: 1px #006d6d solid;
    }
    .button.secondary a {
        background: transparent;    
    }
    .button a:hover {
        text-decoration: none;
    }
    .button:not(.secondary) a:hover {
        background: #006d6d;
        border-color: #005959;
    }
    .button.secondary a:hover {
        color: #ddd;
    } 
}
/* Desktop menu */
@media all and (min-width: 900px) {
    .item {
        display: block;
        width: auto;
    }
    .toggle {
        display: none;
    }
    .logo {
        order: 0;
    }
    .item {
        order: 1;
    }
    .button {
        order: 2;
    }
    .menu li {
        padding: 15px 10px;
    }
    .menu li.button {
        padding-right: 0;
    }
}
</style>
<nav>
    <ul class="menu">
        <li class="toggle"> <a href="#"><i class="fas fa-bars"></i></a> fadfsadf </li>
        <li class="logo"><a href="#">Invisible App</a></li>
        <li class="item"><a href="#">Home</a></li>
        <li class="item"><a href="#">About</a></li>
        <li class="item"><a href="#">Services</a></li>
        <li class="item"><a href="#">Features</a></li>
        <li class="item"><a href="#">Blog</a></li>
        <li class="item"><a href="#">Contact</a>
        </li>
        <li class="item button"><a href="#">Log In</a></li>
        <li class="item button secondary"><a href="#">Sign Up</a></li>

    </ul>
</nav>

<main>
</main>
<script>

$(function() {
    console.log("here");
    $(".item").on("click", function() {
        console.log("clicked");
        if ($(".item").hasClass("active")) {
            $(".item").removeClass("active");
            $(this).find("a").html("<i class='fas fa-bars'></i>");
        } else {
            $(".item").addClass("active");
            $(this).find("a").html("<i class='fas fa-times'></i>");
        }
    });
});
</script>

复制以上代码并将其粘贴到编辑器中,然后将其保存为test.html并在浏览器上运行,然后单击jquery现在正在工作的任何菜单,并且菜单也隐藏。首先应添加Jquery js。希望对您有帮助