Can I get a Javascript event from the selection of text outside of text or textarea?

前端 未结 3 1786
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-15 19:42

I would like to know when a user selects text in an html page using Javascript. The text should not be editable. The onselect event seems to be only applicabl

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-15 20:09

    Sure, an example exists here: http://www.codetoad.com/javascript_get_selected_text.asp

    Using what you see here, you could bind events to the click/release events of the document body, and check to see if there is a selection, and how long the selection is to determine if they've selected any text.

    StackOverflow Archive:

    1. What's the event fired when a user selects text on a page?
    2. Javascript to get Paragraph of Selected Text in WebPage

提交回复
热议问题