jQuery Toggle类问题
有两个按钮应切换类以显示或隐藏两个Divs。仅应同时开放一个DIV。如果一个DIV打开并单击第二个按钮,则打开一个按钮应在另一个按钮打开之前关闭。我做错了什么,请有人可以帮我吗?
$('.clicker').click(function() {
$('.shop_container').find('.wrapper');
$('.wrapper').toggleClass('show');
$('.shop_container').find('.wrapper.show').removeClass('show');
});
.wrapper {
width: 100vw;
left: -100vw;
height: 100vh;
position: fixed !important;
top: 0px;
z-index: 99999;
background: #ff0000;
transition: all 0.4s ease-in-out;
overflow-y: scroll;
overflow-x: hidden;
}
.wrapper.show {
left: 0px;
background-color: var(--blue);
transition: all 0.4s ease-in-out;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="shop_container">
<div class="clicker">
First
</div>
<div class="clicker">
Second
</div>
<div class="wrapper first">
Content first
</div>
<div class="wrapper second">
Content second
</div>
</div>
There are two buttons which should toggle class for showing or hiding two divs. Only one div should be open at same time. If one div is open and the second button is clicked then the open one should close before the other one opens. Anything i am doing wrong, please can somebody help me?
$('.clicker').click(function() {
$('.shop_container').find('.wrapper');
$('.wrapper').toggleClass('show');
$('.shop_container').find('.wrapper.show').removeClass('show');
});
.wrapper {
width: 100vw;
left: -100vw;
height: 100vh;
position: fixed !important;
top: 0px;
z-index: 99999;
background: #ff0000;
transition: all 0.4s ease-in-out;
overflow-y: scroll;
overflow-x: hidden;
}
.wrapper.show {
left: 0px;
background-color: var(--blue);
transition: all 0.4s ease-in-out;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="shop_container">
<div class="clicker">
First
</div>
<div class="clicker">
Second
</div>
<div class="wrapper first">
Content first
</div>
<div class="wrapper second">
Content second
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码:
结果,您将
show
类添加到具有类包装器
的所有DIV标签,然后再次删除它们。您可以使用
数据集
与:Your code :
As a result, you add the
show
class to all the div tags that have the classwrapper
and then remove them again.You can use
dataset
same as :