jQuery toggle类不响应
我正在尝试使用toggleclass构建汉堡菜单。我希望可以单击它来通过添加active的类来显示菜单。默认情况下,我的汉堡图标没有显示,然后添加活动类应该使其显示和可单击。我已经尝试了几种不同的方式,但没有回应,我想知道我在做什么错,任何建议会有所帮助!
const menuToggle = document.querySelector('.menu');
$(document).ready(function() {
$('.card').on('mouseenter', function() {
$(this).find(".inner-card").toggleClass('active');
});
$('.card').on('mouseleave', function() {
$(this).find(".inner-card").toggleClass('active');
});
// #1
$('.hamburger-menu').on('click', function() {
$('.hamburger-menu').toggleClass('.active')
});
//#2
$('.hamburger-menu').onclick(function() {
$('.hamburger-menu').toggleClass('.active')
});
});
.hamburger-menu {
display: none;
}
@media only screen and (max-width: 886px) {
header {
display: flex;
justify-content: space-between;
}
.hamburger-menu {
display: inline-block;
.menu.active {
height: 100%;
width: 350px;
background: white;
z-index: 10;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<header>
<nav class="nav-links">
<a href="http://" class="nav-branding">TravelSite</a>
<form id="form">
<input type="search" class="searchbar" id="query" name="q" placeholder="Search...">
<button>Search</button>
</form>
<ul class="nav-ul">
<li><a href="http://">Destinations</a></li>
<li><a href="http://">Blog</a></li>
<li><a href="http://">About</a></li>
<li><a href="http://">Contact</a></li>
</ul>
<div class="hamburger-menu"> <img src="../static/img/burger-bar.png" alt=""></div>
</nav>
</header>
I am trying to use toggleclass to build a hamburger menu. I want it to be clickable to display the menu by adding the class active. By default I have the burger icon display none and then adding the active class is supposed to make it display and clickable. I've tried the code a few different ways and it is not responding I wonder what I am doing wrong, any advice helps!
const menuToggle = document.querySelector('.menu');
$(document).ready(function() {
$('.card').on('mouseenter', function() {
$(this).find(".inner-card").toggleClass('active');
});
$('.card').on('mouseleave', function() {
$(this).find(".inner-card").toggleClass('active');
});
// #1
$('.hamburger-menu').on('click', function() {
$('.hamburger-menu').toggleClass('.active')
});
//#2
$('.hamburger-menu').onclick(function() {
$('.hamburger-menu').toggleClass('.active')
});
});
.hamburger-menu {
display: none;
}
@media only screen and (max-width: 886px) {
header {
display: flex;
justify-content: space-between;
}
.hamburger-menu {
display: inline-block;
.menu.active {
height: 100%;
width: 350px;
background: white;
z-index: 10;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<header>
<nav class="nav-links">
<a href="http://" class="nav-branding">TravelSite</a>
<form id="form">
<input type="search" class="searchbar" id="query" name="q" placeholder="Search...">
<button>Search</button>
</form>
<ul class="nav-ul">
<li><a href="http://">Destinations</a></li>
<li><a href="http://">Blog</a></li>
<li><a href="http://">About</a></li>
<li><a href="http://">Contact</a></li>
</ul>
<div class="hamburger-menu"> <img src="../static/img/burger-bar.png" alt=""></div>
</nav>
</header>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您几乎在那儿,但您的目标是错误的项目。我对您的HTML进行了一些重新安排,并添加了一个类以包含链接,以便您可以定位该链接,而不是
ul
。您在CSS中遇到了一些格式问题,因此我只是将媒体查询并清理干净,可以将其添加到任何需要的尺寸中,然后从那里进行调整。You were almost there but you were targeting the wrong item. I did some rearranging to your HTML and added a class to contain the links so you could target that instead of the
ul
. You had some formatting issues in your CSS so I just took the media query out and cleaned it up, you can add that back into whatever size you need and adjust it from there.