简体   繁体   English

Onclick应该关闭其他打开的元素

[英]Onclick should close other opened element

I do have 2 elements in my page which i want to show them when user clicks on 2 different buttons. 我的页面中确实有2个元素,当用户单击2个不同的按钮时,我想显示它们。

Here is my code 这是我的代码

$('.social-toggle').on('click', function() {
  $(this).next().toggleClass('open-menu');
});

$('.social-toggle1').on('click', function() {
  $(this).next().toggleClass('open-menu');
});

When user clicks on the first button, the first element will be show up, but i want either user clicks on the second button or one of the links in the opened-menu the first opened menu be closed. 当用户单击第一个按钮时,将显示第一个元素,但是我希望用户单击第二个按钮或打开菜单中的链接之一,关闭第一个打开的菜单。

JSFIDDLE 的jsfiddle

You do not need to write two different event here. 您无需在此处编写两个不同的事件。 Use comma seprated multiple selector for targetting both button: 使用逗号分隔的多个选择器定位两个按钮:

$('.social-toggle,.social-toggle1').on('click', function() {
  $('.social-networks').not($(this).next()).removeClass('open-menu');
  $(this).next().toggleClass('open-menu');
});

Working Demo 工作演示

Here is the code what you want. 是您想要的代码。 You didnt removed the class which was applied previously. 您没有删除以前应用的类。

$('.social-toggle').on('click', function() {
    $('.social-toggle1').next().removeClass('open-menu');
  $(this).next().toggleClass('open-menu');

});

$('.social-toggle1').on('click', function() {
    $('.social-toggle').next().removeClass('open-menu');
  $(this).next().toggleClass('open-menu');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM