Locating DOM element by absolute coordinates

后端 未结 3 1976
刺人心
刺人心 2020-12-16 18:45

Is there a simple way to locate all DOM elements that \"cover\" (that is, have within its boundaries) a pixel with X/Y coordinate pair?

3条回答
  •  既然无缘
    2020-12-16 18:54

    I couldn't stop myself to jump on Felix Kling's answer:

    var $info = $('
    ', { css: { position: 'fixed', top: '0px', left: '0px', opacity: 0.77, width: '200px', height: '200px', backgroundColor: '#B4DA55', border: '2px solid black' } }).prependTo(document.body); $(window).bind('mousemove', function(e) { var ele = document.elementFromPoint(e.pageX, e.pageY); ele && $info.html('NodeType: ' + ele.nodeType + '
    nodeName: ' + ele.nodeName + '
    Content: ' + ele.textContent.slice(0,20)); });

    updated: background-color !

提交回复
热议问题