jQuery 下拉问题(无法保持子菜单可见)

发布于 2025-01-07 08:29:23 字数 1152 浏览 3 评论 0原文

我正在使用 jQuery 构建一个小下拉菜单,但我有一个小问题。 当我进入菜单时,它会下降,但是当我想用鼠标转到下拉部分时,它会返回(隐藏)。 这是因为菜单的标记与大多数 jQuery 下拉菜单有点不同(我认为)。

如果用户将鼠标悬停在 a 标签上,菜单应该可见,但由于 div 标签不是 a 标签的子标签,因此如果用户离开 a 标签,菜单将会淡出。有办法解决这个问题吗?我在网上找不到任何关于此的信息。

菜单标记

     <ul>
        <li>
            <span>Some text</span>
            <a href="#"><img src="icon.png"/></a>
            <div>here comes the dropdown list</div>
        </li>
        <li>
            <span>Some text</span>
            <a href="#"><img src="icon.png"/></a>
            <div>here comes the dropdown list</div>
        </li>
        <li>
            <span>Some text</span>
            <a href="#"><img src="icon.png"/></a>
            <div>here comes the dropdown list</div>
        </li>
    </ul>

jQuery 代码标记

    $('a').hover(function(){                     

        $(this).next('div').fadeIn(200);

    },function(){

        $(this).next('div').fadeOut(200);

    });

I am building a small dropdown menu with jQuery, but i am have a small issue.
When I enter the menu it drops down, but when i want to go to the dropdown part with my mouse it will go back up(hide).
This is because the markup of the menu is a little bit different than most jQuery dropdown menus(i think).

The menu should be visible if a user hovers the a tag, but because the div tag isn't a child of the a tag so it will fadeout if a user leave the a tag. Is there a way to fix this? I cant find any thing about this on the web.

Menu markup

     <ul>
        <li>
            <span>Some text</span>
            <a href="#"><img src="icon.png"/></a>
            <div>here comes the dropdown list</div>
        </li>
        <li>
            <span>Some text</span>
            <a href="#"><img src="icon.png"/></a>
            <div>here comes the dropdown list</div>
        </li>
        <li>
            <span>Some text</span>
            <a href="#"><img src="icon.png"/></a>
            <div>here comes the dropdown list</div>
        </li>
    </ul>

jQuery code markup

    $('a').hover(function(){                     

        $(this).next('div').fadeIn(200);

    },function(){

        $(this).next('div').fadeOut(200);

    });

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡離愁欲言轉身 2025-01-14 08:29:23

淡出不应该绑定到锚点,而应该绑定到 div。

您可以在锚点悬停时开始显示 div:

$('a').on("mouseenter", function() {
  $(this).next('div').fadeIn(200);
});

然后在鼠标离开时隐藏 div:

$('div').on("mouseleave", function() {
  $(this).fadeOut(200);
});

The fade out shouldn't be bound to the anchor but to the div.

You can begin to show the div when anchor is hovered:

$('a').on("mouseenter", function() {
  $(this).next('div').fadeIn(200);
});

Then hide the div when mouse leaves it:

$('div').on("mouseleave", function() {
  $(this).fadeOut(200);
});
掩耳倾听 2025-01-14 08:29:23

这是因为您的悬停事件针对的是链接元素,当您将鼠标悬停在链接之外时,您会隐藏菜单项。

要防止这种行为,只需将父元素作为目标即可,在本例中为

  • :

    $('li').hover(function(){
    
        $(this).children('div').fadeIn(200);
    
    },功能(){
    
        $(this).children('div').fadeOut(200);
    
    });
    
  • This is because your hover event is targeting the link element, when you hover out of the link you hide the menu item.

    To prevent this behaviour, simply target the parent element instead, in this case the

  • :

    $('li').hover(function(){
    
        $(this).children('div').fadeIn(200);
    
    },function(){
    
        $(this).children('div').fadeOut(200);
    
    });
    
  • ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文