javascript - 当鼠标不在链接上时,下拉菜单消失

标签 javascript jquery css html

所以我尝试使用 Jquery 制作一个下拉菜单,但是当您尝试选择第二个链接时,菜单消失了。另外,我该如何使其一次只能出现一个下拉菜单?

我需要如何更改它才能修复它?

$('#nav a').bind('mouseover', openSubMenu);
$('#nav ul ul').bind('mouseout', closeSubMenu);
function openSubMenu(){
    $(this).parent().find('ul').slideDown(300);
};
function closeSubMenu(){
    $(this).closest('ul').slideUp(300);
};

示例在这里:https://jsfiddle.net/g36tywLc/

最佳答案

使用mouseleave代替:

$('#nav ul ul').bind('mouseleave', closeSubMenu);

这是更新的fiddle .

来自W3schools :

The mouseout event triggers when the mouse pointer leaves any child elements as well the selected element.
The mouseleave event is only triggered when the mouse pointer leaves the selected element.

关于javascript - 当鼠标不在链接上时,下拉菜单消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30426876/

相关文章:

javascript - JQuery 错误地追加创建的 JQuery 对象

javascript - JS - 更改对象所有实例的值

jquery - 使用 jQuery 更新 ViewBag 并渲染结果

jquery - 使用 FitText jquery 插件调整文本大小

jquery - 在实现 CSS 中单击按钮时不会触发模态

php - 创建网格而不是表格

javascript - 未捕获的类型错误 : Cannot create property 'guid' on string

javascript - 使用 yii2 表单等待 ajax 响应

javascript - 获取TinyMce( Orchard )中元素的z索引

jquery - 选择搜索多个单词