简体   繁体   English

jQuery toggle()并不总是触发

[英]jQuery toggle() not always firing

I've been making some basic mobile navigation and am using a click event to show/hide the menu. 我一直在进行一些基本的移动导航,并且正在使用click事件来显示/隐藏菜单。

A reduced code sample: 简化的代码示例:

        jQuery('.menu-button').click(function(){
            jQuery('.header-nav').toggle();
            console.log('clicked');
        });

I've been remotely debugging on mobile and the console.log always works, but the .header-nav toggle() seems to randomly not trigger - I can't spot a pattern to it, but it always remains in the DOM (which it should), so it being somehow removed is not the reason why it is not firing. 我已经在移动设备上进行了远程调试,console.log始终可以正常工作,但是.header-nav toggle()似乎不会随机触发-我无法找到模式,但它始终保留在DOM中(它应该),因此将其以某种方式删除并不是它不触发的原因。

Any ideas? 有任何想法吗?

Thanks to Kevin B's comment it seems that the click event is firing multiple times. 感谢Kevin B的评论,单击事件似乎多次触发。 To fix this, the following was used: 为了解决这个问题,使用了以下方法:

$(element).off().on('click', function() {
    // function body
});

Reference: jQuery click events firing multiple times 参考: jQuery单击事件多次触发

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

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