简体   繁体   English

如何使用 Javascript 从鼠标悬停事件中排除元素?

[英]How can I exclude an element from a mouseover event with Javascript?

I'm basically trying to build the equivalent of the firebug inspector in the browser.我基本上是在尝试在浏览器中构建相当于萤火虫检查器的东西。 I'm currently using the <body> as the container in this example:我目前在此示例中使用<body>作为容器:

container.onmouseover = function(ev) { inspect(ev.target); }

In the inspect method, I reposition and resize a semi-transparent fixed positioned element over the element returned from the event target.在检查方法中,我在从事件目标返回的元素上重新定位和调整半透明固定定位元素的大小。 But this causes the mouseover event to subsequently inspect the overlay element itself.但这会导致mouseover事件随后检查覆盖元素本身。

I basically want the event handler to disregard the overlaid element completely (it only exists to provide visual feedback) and continue to fire onmouseover events for the elements detected beneath it.我基本上希望事件处理程序完全忽略覆盖的元素(它只是为了提供视觉反馈而存在)并继续为在其下方检测到的元素触发onmouseover事件。 Does this make sense?这有意义吗?

If you don't have to support all browsers then you could use the css property pointer-events .如果您不必支持所有浏览器,那么您可以使用 css 属性pointer-events When set to none all events will be ignored.当设置为 none 时,所有事件都将被忽略。 Try toggling pointer-events in this fiddle and see what I mean.尝试在这个小提琴中切换指针事件,看看我的意思。

http://jsfiddle.net/zDygA/ http://jsfiddle.net/zDygA/

Browser support浏览器支持

pointer-events:none;

I hate to link out, but this is a good article if you need to support IE.我讨厌链接出去,但是如果您需要支持 IE,这是一篇很好的文章。

http://www.vinylfox.com/forwarding-mouse-events-through-layers/ http://www.vinylfox.com/forwarding-mouse-events-through-layers/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在JavaScript中以编程方式调用mouseover事件处理程序 - How can I call mouseover event handler programmatically in JavaScript 如何在jquery .click()事件中将元素类排除为目标? - How can I exclude an element class from being a target in jquery .click() event? 如何从 Javascript 中的元素中删除点击事件? - How can I remove a click event from an element in Javascript? 如何使用 jquery 模拟鼠标悬停事件? - How can I simulate a mouseover event with jquery? 由于我已将mouseover和mouseout事件附加到HTML中的每个元素,如何在几个HTML元素上阻止相同的事件? - Since I have attached mouseover and mouseout event to each and every element in HTML, How can I prevent same event on few HTML element? 如何使用JavaScript仅触发img元素触发鼠标悬停事件 - how to trigger mouseover event only for an img element using javascript 使用鼠标悬停事件更改 JavaScript 中元素的颜色 - Change color of an element in JavaScript with mouseover event 如何从继承的链接中排除此元素? - How can I exclude this element from an inherited link? 如何使用javascript / jquery从任何地方禁用鼠标悬停事件? - How to disable mouseover event from everywhere using javascript/jquery? 如何防止JavaScript多次应用mouseover事件 - How to prevent JavaScript from applying the mouseover event several times
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM