Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery-应用类时刷新dom?_Javascript_Jquery - Fatal编程技术网

Javascript jQuery-应用类时刷新dom?

Javascript jQuery-应用类时刷新dom?,javascript,jquery,Javascript,Jquery,如果用户在移动设备或桌面上加载页面,我会在主体上应用一个类来控制megamenu的工作方式,问题是当以这种方式添加类时,不会发生任何事情,因为我认为它们不在dom中,所以不会触发,有没有办法解决这个问题 if (Modernizr.mq('only all and (max-width: 599px)')) { $('body').removeClass('desktop').addClass('mobile'); } if (Modernizr.mq('only all and (m

如果用户在移动设备或桌面上加载页面,我会在主体上应用一个类来控制megamenu的工作方式,问题是当以这种方式添加类时,不会发生任何事情,因为我认为它们不在dom中,所以不会触发,有没有办法解决这个问题

if (Modernizr.mq('only all and (max-width: 599px)')) {
    $('body').removeClass('desktop').addClass('mobile');
}

if (Modernizr.mq('only all and (min-width: 600px)')) {
    $('body').removeClass('mobile').addClass('desktop');
}


$('.mobile .navigation nav > ul > li').on({
       mouseenter: function (e) {
          $(this).find('.dropdown').delay(200).slideDown();
       }
});

$('.desktop .navigation nav > ul > li').on({
      mouseenter: function (e) {
          $(this).find('.dropdown').delay(200).addClass("hovered");
      },
      mouseleave: function (e) {
          $(this).find('.dropdown').removeClass("hovered");
      }
});

如果我答对了,您的
如果(modernizer.mq('only all and(max width:599px)){
只检查mediaquery,没有附加事件

如果要在调整浏览器大小时检查宽度,请尝试以下操作:

$(window).resize(function() {
    if (Modernizr.mq('only all and (max-width: 599px)')) {
        $('body').removeClass('desktop').addClass('mobile');
    }

    if (Modernizr.mq('only all and (min-width: 600px)')) {
        $('body').removeClass('mobile').addClass('desktop');
    }
});

您是否尝试过将整个内容包装在
$(文档)中。准备好了吗?
?无法解决问题。如果页面加载时应用了desktop类,并说“我”然后将屏幕大小更改为“移动”,则移动功能不起作用,反之亦然(我应该注意到,还有一段代码可以在屏幕调整大小时切换类)然后您需要一个事件来侦听browser resize,并将所有需要再次运行的代码放在其中(您可能希望对其进行限制,以便在用户仍在调整大小时不会触发多次)