jquery - 在 jquery 中显示的子菜单

标签 jquery html css drop-down-menu submenu

这是测试站点链接。我遇到了显示子菜单的问题 link

在导航栏“为什么斯德哥尔摩”中,当我将其悬停时,还会显示子菜单,当我将其悬停在子菜单上时,它的工作正常,我做错了什么,非常感谢..

(function($){

    //cache nav
    var nav = $("#topNav");

    //add indicator and hovers to submenu parents
    nav.find("li").each(function() {
        if ($(this).find("ul").length > 0) {
            $("<span>").text("").appendTo($(this).children(":first"));

            //show subnav on hover
            $(this).mouseenter(function() {
                $(this).find("ul").stop(true, true).slideDown();
            });

            //hide submenus on exit
            $(this).mouseleave(function() {
                $(this).find("ul").stop(true, true).slideUp();
            });
        }
    });
})(jQuery);

最佳答案

您应该使用 children() 而不是 find()。当您使用 find('ul') 时,它会使用您的选择器中的所有 ul

Here is working jsFiddle.

        //show subnav on hover
        $(this).mouseenter(function() {
            $(this).children("ul").stop(true, true).slideDown();
        });

        //hide submenus on exit
        $(this).mouseleave(function() {
            $(this).children("ul").stop(true, true).slideUp();
        });

关于jquery - 在 jquery 中显示的子菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14594373/

相关文章:

html - 使用文本和图像样式提交按钮

javascript - 使用 JavaScript 更改大量样式

javascript - 光滑的 slider 自定义点

python - 错误 404 - 使样式表在 Bottle 中工作(Python Web 框架)

html - 隐藏表格但在 CSS 中显示边框

html - Bootstrap 汉堡包无法按预期工作

css - 缩放时背景图像会切断图像顶部

javascript - Jquery - 一个 AJAX 调用的多个函数调用

jquery - Bootstrap 3 轮播 : set unidirectional (vertical) transition

jquery - 打开新 div 时关闭 div(jQuery 幻灯片)