Getting DIV id based on x & y position

后端 未结 9 842
渐次进展
渐次进展 2020-12-06 11:52

The problem I\'m trying to solve is \"What\'s at this position?\"

It\'s fairly trivial to get the x/y position (offset) of a DIV, but what about the reverse? How do

9条回答
  •  借酒劲吻你
    2020-12-06 12:24

    function getDivByXY(x,y) {
       var alldivs = document.getElementsByTagName('div');
    
       for(var d = 0; d < alldivs.length; d++) {
          if((alldivs[d].offsetLeft == x) && (alldivs[d].offsetTop == y)) {
             return alldivs[d];
          }
       }
    
       return false;
    }
    

提交回复
热议问题