Get the Highlighted/Selected text

后端 未结 5 1577
無奈伤痛
無奈伤痛 2020-11-21 06:34

Is it possible to get the highlighted text in a paragraph of a website e.g. by using jQuery?

5条回答
  •  难免孤独
    2020-11-21 07:21

    This solution works if you're using chrome (can't verify other browsers) and if the text is located in the same DOM Element:

    window.getSelection().anchorNode.textContent.substring(
      window.getSelection().extentOffset, 
      window.getSelection().anchorOffset)
    

提交回复
热议问题