javascript - 在切换类上的 mouseenter 上

标签 javascript jquery html css

当我调整浏览器大小时,我使用一点 jquery 来更改菜单的类。这行得通,但是当我想在新类上使用 mouseenter 时,它不起作用。

这是我调整大小的代码:

$( window ).resize(function() {
    windowWidth = $(window).width();
    windowWidth = windowWidth + 17;

    if(windowWidth < 780) {
        $('.menu_bar').parent().addClass('mobile-header');
        $('.nav_top').removeClass('nav_top').addClass('nav-left');
        $('.primary-content').addClass('responsive');
    } else {
        $('.menu_bar').parent().removeClass('mobile-header');
        $('.nav-left').removeClass('nav-left').addClass('nav_top');
        $('.primary-content').removeClass('responsive');
    }

});

这是鼠标输入的代码:

$(".nav-left ul").on({
        mouseenter: function () {
            $('ul', this).stop(true, true).slideDown(100);
        },
        mouseleave: function () {
            $('ul', this).stop(true, true).slideUp(100);
        }
    }, "li");

如果我在我的标记中将 nav-top 更改为 nav-left(当菜单在文档上准备好类 nav-left 时)mouseenter 工作正常。

最佳答案

试试这个:

$( window ).resize(function() {
windowWidth = $(window).width();
windowWidth = windowWidth + 17;

if(windowWidth < 780) {
    $('.menu_bar').parent().addClass('mobile-header');
    $('.nav_top').removeClass('nav_top').addClass('nav-left');
    $('.primary-content').addClass('responsive');
} else {
    $('.menu_bar').parent().removeClass('mobile-header');
    $('.nav-left').removeClass('nav-left').addClass('nav_top');
    $('.primary-content').removeClass('responsive');
}

$(".nav-left ul").on({
    mouseenter: function () {
        $('ul', this).stop(true, true).slideDown(100);
    },
    mouseleave: function () {
        $('ul', this).stop(true, true).slideUp(100);
    }
}, "li");

});

关于javascript - 在切换类上的 mouseenter 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20555196/

相关文章:

带有 flot 和 AJAX 导航的 jQuery Mobile

javascript - 如何用 jQuery 替换 innerHTML?

html - 我如何在按钮菜单上添加声音

javascript - jquery 元素 id 未正确更改

JavaScript基于域名的重定向

javascript - 为什么我的网站在显示样式页面之前显示没有样式的元素?

如果小于则 JavaScript 不工作

android - mailto: 和 tel: 在移动网络上的 anchor 标记中带有图像的方案

javascript - jQuery 返回 ID 以某个字符串开头的元素

javascript - 当我确定我的函数已定义时,为什么我在错误控制台上看到 'function not defined'?