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

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

    This is not easy to do in a cross-browser way. In IE only the select event applies to body text as well as form inputs so would do what you want, but to detect when the user has made a selection in a cross-browser way you will need to handle both keyup and mouseup events. Even then you won't be detecting selection events such as the user using the "Select all" menu option (usually found in the Edit and right click context menus). So you're down to polling at regular intervals and checking properties of the selection object (obtained via window.getSelection() or document.selection in IE).

提交回复
热议问题