选择 DOM 对象的父对象,并排除所选父对象的子对象
我想在用户点击保存我的应用程序的 DOM 对象时显示一个对话框。
我在想,我只需为所有家长添加一次点击事件,然后触发与之对话。
因为包含了我获取父对象的 DOM 对象,所以即使单击它自身的对象,它也会触发对话。
本质上,我想在父级中打一个洞,以排除用户正在与之交互的 DOM。
我想 mouseleave 然后单击任何其他 dom 对象都会起作用。
有什么建议吗?
I'd like to display a dialogue when a user clicks out of the DOM object that holds my app.
I was thinking, I'd just put a one time click event on all parents and then trigger the dialogue with that.
Because the DOM object that I'm getting the parent's for is included, it's triggering the dialogue even when the object it's self is clicked.
I want to, in essence, punch a hole in the parents to exclude the DOM that the user is interacting with.
I was thinking mouseleave and then click any other dom object would work.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
说明:您检查事件目标是否等于#container 元素。如果不是,则意味着单击事件从 #container 的子级之一中冒出来,您只需通过返回来终止该事件。如果是,那么您就按照您的意愿行事。
您可以在我的网站 (w3viewer.com) 上查看其工作原理。单击左下角的“关于”链接。将会弹出一个框。要关闭该框,您必须在其外部单击。
Explanation: You check whether the event target is equal to the #container element. If not, that means that the click event bubbled up from one of the #container's children, and you just kill the event by returning. If yes, you do what you intended.
You can view how this works on my web-site (w3viewer.com). Click on the "About" link in the bottom left corner. A box will pop up. To close the box, you have to click outside of it.