我可以从文本或textarea之外的文本选择中获取Javascript事件吗?

我可以从文本或textarea之外的文本选择中获取Javascript事件吗?

问题描述:

我想知道用户何时使用Javascript在html页面中选择文本。该文本不应该是可编辑的。 onselect 事件似乎仅适用于< textarea> <输入类型=TEXT> 标签。如果任一标签被禁用,则不会触发该事件。

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 applicable to <textarea> and <input type="TEXT"> tags. The event is not fired if either tag is disabled.

这些标签有解决方法吗?

Is there a way around this with these tags?

有没有完全不同的方法?

Is there a completely different approach?

当然,这里有一个例子: http://www.codetoad.com/javascript_get_selected_text.asp

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.


  1. 当用户选择页面上的文字时,该事件是什么?

  2. Javascript获取网页中所选文本的段落

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