Get DOM text node from point?

前端 未结 4 1979
无人共我
无人共我 2020-12-30 06:49

Just like I can get an element from a point with document.elementFromPoint or document.getElementFromPoint, is it possible to somehow get a text no

4条回答
  •  猫巷女王i
    2020-12-30 07:33

    Considering this document (fiddle):

    
    
        some text here 
        

    lalala

    bla bla ​

    And this code:

    $(document).on('click', function(evt) {
        var elem = document.elementFromPoint(evt.clientX, evt.clientY);
        console.log(elem);
    });
    

    When you click anywhere inside the

    tag, the tag element itself is logged. However, when the surrounding text is clicked, the is returned because text fragments are not considered elements.

    Conclusion

    It's not possible to accomplish what you want with elementFromPoint() and because text fragments don't receive click events, I don't think it's possible at all.

提交回复
热议问题