简体   繁体   English

jQuery切换无法在手机上工作

[英]jquery toggle not working on mobile

so i am trying to make it so when you click menu on the mobile responsive version of this site right here http://dev.trafficdigitalagency.com/stage/ it toggles what is display:none; 因此,我试图做到这一点,因此,当您单击此站点的移动响应版本上的菜单时,此处http://dev.trafficdigitalagency.com/stage/可以切换显示内容:无; in the class "sub-menu" 在“子菜单”类中

here is the javascript/jquery I use (which can be found at http://dev.trafficdigitalagency.com/stage/js.js ) 这是我使用的javascript / jquery(可以在http://dev.trafficdigitalagency.com/stage/js.js上找到)

$(document).ready(function(){
  $("#menu-item-3121").click(function() {
    $(".sub-menu").fadeToggle("slow");
  });
});

why when i click on menu in the responsive version does the sub menu not toggle? 为什么当我在响应版本中单击菜单时,子菜单不会切换?

I had the same issue the other day. 前几天我遇到了同样的问题。 Turns out the click event callback was being set up to fire twice, so the toggle looked like it wasn't firing at all. 事实证明,将点击事件回调设置为触发两次,因此切换似乎根本没有触发。 Ended up having to ensure any existing listeners are removed before adding one back. 最终必须确保删除所有现有的侦听器,然后再添加一个侦听器。 Note the off() call. 请注意off()调用。 Hope this works for you: 希望这对您有用:

$(document).ready(function(){
    $("#menu-item-3121").off("click").on("click", function() {
        $(".sub-menu").fadeToggle("slow");
    });
})

If you take a look at the console, (Ctrl-Shift-J in Chrome) where all JS erros all logged, you will see that the real problem relies on the way WordPress is loading the jQuery library in 'no conflict' mode. 如果您查看所有JS错误均已记录的控制台(Chrome中的Ctrl-Shift-J),您会发现真正的问题取决于WordPress以“无冲突”模式加载jQuery库的方式。

I believe the solution provided here by @RedEyedMonster will help you, so write your function as this: 我相信@RedEyedMonster 在此提供的解决方案将为您提供帮助,因此,您可以这样编写函数:

jQuery(document).ready(function ($) {
    $("#menu-item-3121").click(function() {
        $(this).find(".sub-menu").fadeToggle("slow");
    });
});

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

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