这是测试站点链接。我遇到了显示子菜单的问题 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
。
//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/