在 jquery 中的 onblur 事件中隐藏上下文菜单
是否有人尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想知道焦点何时离开容器区域,但又不想让容器内的子控件触发事件,请使用
mouseleave
。在这种情况下,
mouseout
或blur
不是您所需要的,因为当容器内的任何子控件接收到鼠标焦点时,它们都会触发,从而导致包含它们的菜单隐藏。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
.mouseout
orblur
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.将模糊与回调一起使用。
但它还没有经过测试。您想恢复其他模糊的右键功能吗?
我认为这在其他类型的活动中会更好地执行。
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.
您明确提到了
blur
事件,但我认为这实际上并不是您所需要的,因为您提到的上下文菜单div
可能永远不会聚焦或模糊。您应该使用 mouseout 事件:
假设您的上下文菜单有一个 id 'contextMenuContainer',这应该覆盖它:
有关更多信息,请参阅 jQuery 事件/mouseout 文档。
更新:
我尝试在您链接到的插件页面上注册一个 mouseout 事件处理程序,并且它运行得很好。我应该注意,每次更改菜单项时它都会触发,因此您需要检查事件目标以确保鼠标实际上已退出整个菜单。
You mention the
blur
event explicitly, but I don't think that's actually what you need, since the context menudiv
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:
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.