确定js中event.target下面的元素
我正在尝试确定鼠标当前在 OpenLayers 地图中悬停的图块。当前编写的代码(使用标准悬停处理程序并简单地发送回事件)在 Chrome 中有效,但在 Firefox 中失败,因为 SVG 元素位于图块和鼠标之间。我确信使用 VML 的 IE 中也会发生类似的情况。
因此,问题是,如果鼠标所在的元素被当前作为该请求目标的另一个元素“阻止”,如何确定该元素?或者,是否可以在另一个 div 中使用 trigger()
“重新运行”事件,并检索新目标来模拟相同的事件?否则,使用 OpenLayers 确定位于某个像素坐标处的图像会很困难且效率低下。
I'm attempting to determine the tile that the mouse is currently hovering over in an OpenLayers map. The code that currently is written - that uses a standard Hover handler and simply sends back the event - works in Chrome but fails in Firefox, because an SVG element comes in between the tile and the mouse. I'm sure that something similar could happen in IE with VML.
So, the question is, how does one determine the element that a mouse is over if that element is 'blocked' by another element that is currently the target of that request? Or, is it possible to 're-run' an event using trigger()
in another div, and retrieve the new target to simulate the same? Otherwise, it's difficult and inefficient to determine the image that lies at a certain pixel coordinate using OpenLayers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AFAIK,没有有效的方法来确定哪些元素位于鼠标下方;您必须从事件 (
event.pageX/Y
) 获取 x/y 位置,并将其与页面上每个元素的位置/大小进行比较。AFAIK, there is no efficient way to determine which elements are under the mouse; you must get the x/y position from the event (
event.pageX/Y
) and compare that with the position/size of each element on the page.我不确定,但也许 document.elementFromPoint 方法可以帮助您,请参阅:
elementFromPoint 方法
I'm not sure but maybe the document.elementFromPoint method can help you, see:
elementFromPoint method