基于x& D获取DIV id你的位置 [英] Getting DIV id based on x & y position
问题描述
我试图解决的问题是这个位置是什么?
The problem I'm trying to solve is "What's at this position?"
获得DIV的x / y位置(偏移量)是相当简单的,但反过来呢?如何获得给定x / y位置的DIV(或任何元素)的id?
It's fairly trivial to get the x/y position (offset) of a DIV, but what about the reverse? How do I get the id of a DIV (or any element) given an x/y position?
推荐答案
不幸的是,触发a制造/模拟鼠标事件将无法工作,因为当您发送它时,您必须提供目标元素。由于那个元素是你想要弄清楚的元素,你所能做的就是将它分配到身体上,好像它已经冒泡了。
Unfortunately, triggering a manufactured/simulated mouse event won't work, since when you dispatch it, you have to provide a target element. Since that element is the one you're trying to figure out, all you could do is dispatch it on the body, as if it had already bubbled.
你真的是左手自己做,即手动浏览你感兴趣的元素,并将它们的位置/大小/ zIndex与你的x / y点进行比较,看看它们是否重叠。除了IE和更近期的FF3,你可以使用
You really are left to do it on your own, that is manually walk through the elements you're interested in, and compare their position/size/zIndex to your x/y point and see if they overlap. Except in IE and more recently FF3, where you can use
var el = document.elementFromPoint(x, y);
参见
http://developer.mozilla.org/En/DOM:document.elementFromPoint
http: //msdn.microsoft.com/en-us/library/ms536417(VS.85).aspx
这篇关于基于x& D获取DIV id你的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!