在 jquery 中的 onblur 事件中隐藏上下文菜单

发布于 2024-08-05 10:22:28 字数 122 浏览 2 评论 0原文

是否有人尝试在 blur 事件中成功隐藏上下文菜单?我想要做的是当鼠标未位于上下文菜单 div 内时隐藏自定义右键菜单。

这使用了 jquery 上下文菜单插件。

Does any one tried to successfully hide the context menu at blur event? What I want to do is to hide the customized right click menu when the mouse is not positioned inside the context menu div.

This uses the jquery context menu plugin.

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

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

发布评论

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

评论(3

和影子一齐双人舞 2024-08-12 10:22:28

如果您想知道焦点何时离开容器区域,但又不想让容器内的子控件触发事件,请使用 mouseleave

$('#menu').on('mouseleave', function(){
  $(this).hide();
});

在这种情况下,mouseoutblur 不是您所需要的,因为当容器内的任何子控件接收到鼠标焦点时,它们都会触发,从而导致包含它们的菜单隐藏。

If you want to know when the focus leaves the area of the container, but not have child controls inside the container trigger an event, use mouseleave.

$('#menu').on('mouseleave', function(){
  $(this).hide();
});

mouseout or blur is not what you need in this scenario, because they will trigger when any child control inside the container receives mouse focus, causing the menu containing them to hide.

梦里南柯 2024-08-12 10:22:28

将模糊与回调一起使用。
但它还没有经过测试。您想恢复其他模糊的右键功能吗?
我认为这在其他类型的活动中会更好地执行。

$("input").blur(function () {
     window.oncontextmenu = function () {
        return false;
     }
});

Use blur with a callback.
It is not tested though. Do you want to restore the right click functionality on other blur?
I think this will be better executed on other types of events.

$("input").blur(function () {
     window.oncontextmenu = function () {
        return false;
     }
});
胡渣熟男 2024-08-12 10:22:28

您明确提到了 blur 事件,但我认为这实际上并不是您所需要的,因为您提到的上下文菜单 div 可能永远不会聚焦或模糊。

您应该使用 mouseout 事件:

假设您的上下文菜单有一个 id 'contextMenuContainer',这应该覆盖它:

$('#contextMenuContainer').mouseout(function() {
    $(this).hide();
});

有关更多信息,请参阅 jQuery 事件/mouseout 文档。

更新:

我尝试在您链接到的插件页面上注册一个 mouseout 事件处理程序,并且它运行得很好。我应该注意,每次更改菜单项时它都会触发,因此您需要检查事件目标以确保鼠标实际上已退出整个菜单。

You mention the blur event explicitly, but I don't think that's actually what you need, since the context menu div you mentioned probably will not ever be focused or blurred.

You should use the mouseout event:

Assuming your context menu has an id of 'contextMenuContainer', this should cover it:

$('#contextMenuContainer').mouseout(function() {
    $(this).hide();
});

For more see the jQuery Events/mouseout documentation.

Update:

I tried registering a mouseout event handler on the plugin page you linked to, and it was firing just fine. I should note that it fires every time you change menu items though, so you'll need to check the event target to make sure the mouse has actually exited the entire menu.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文