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

前端 未结 3 1787
爱一瞬间的悲伤
爱一瞬间的悲伤 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 19:57

    You could capture the mouseUp event, and check if some text is selected using window.getSelection().

    This method may however not be cross-browser compatible (the window.getSelection()).

    EDIT: here is a pretty complete example with mouseUp binding : http://mark.koli.ch/2009/09/use-javascript-and-jquery-to-get-user-selected-text.html

提交回复
热议问题