【发布时间】:2026-02-15 17:30:02
【问题描述】:
$(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;
}
【问题讨论】:
-
您能否在此处使用jsfiddle 或snippet 为此创建small demo 以显示正在发生的问题。另外,添加与之相关的所有css类main
active类。 -
也需要将你的 CSS 添加到问题中
-
在这个特定的代码片段中,没有对 JQuery 的实际引用。您需要打开 F12 控制台并检查 javascript 错误