简体   繁体   English

如何从javascript中具有相同x坐标的document.elementFromPoint获取ID? 我想要所有的div ID是那样的

[英]How to get id from document.elementFromPoint with same x coordinates in javascript? I want all div id's like that

I have two div with same x position. 我有两个具有相同x位置的div。 And i am trying to do that getting id of both divs from document.elementFromPoint(). 我正在尝试从document.elementFromPoint()获取两个div的ID。 Here is the problem that only id recieved of a div. 这是只有id收到div的问题。

 var elem = document.elementFromPoint(50, 50); var elem2 = document.elementFromPoint(50, 250); console.log(elem.id, elem2.id); 
 #box1, #box2 { width: 50px; height: 50px; background: greenyellow; } #box1 { position: absolute; top: 50px; left: 50px; } #box2 { position: absolute; top: 50px; left: 150px; } 
 <div id="box1"></div> <div id="box2"></div> 

You're pretty close, but if you check out the documentation , your parameters are switched round. 您已经很接近了,但是如果您查看文档 ,则会对参数进行轮换。

document.elementFromPoint(x, y)

Where x is left , y is top . x lefty top

 var elem = document.elementFromPoint(50, 50); var elem2 = document.elementFromPoint(150, 50); console.log(elem.id, elem2.id); 
 #box1, #box2 { width: 50px; height: 50px; background: greenyellow; } #box1 { position: absolute; top: 50px; left: 50px; } #box2 { position: absolute; top: 50px; left: 150px; } 
 <div id="box1"></div> <div id="box2"></div> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM