Javascript 中选择的文本事件触发器

如何使用鼠标在页面上 当某人选择给定的文本片段时触发 JavaScript 函数
另外,有没有办法在页面上 查找所选文本的位置

更新: 更明确地说,文本片段可以是一个句子、一个词、一个短语的一部分,也可以是整个段落的一部分。

98427 次浏览

There is no "Text was selected" (DOM) event, but you can bind a mouseup event to the document.body. Within that event handler, you might just check the

document.selection.createRange().text

or

window.getSelection()

methods. There are several topics on Stackoverflow, like this one javascript to get paragraph of selected text in web page.

I'm not sure what you mean with "finding the position", but to stay in my example world you could use the event propertys for X+Y mouse positions.

Example: http://www.jsfiddle.net/2C6fB/1/

Here's a quick mashup:

$('div').mouseup(function() {
var text=getSelectedText();
if (text!='') alert(text);
});


function getSelectedText() {
if (window.getSelection) {
return window.getSelection().toString();
} else if (document.selection) {
return document.selection.createRange().text;
}
return '';
}​


<div>Here is some text</div>

Demo: http://jsfiddle.net/FvnPS/11/

AFAIK, there is no such event you described. But you can emulate that function.

Look over here for the code and demo.

There is a new experimental API that deals with this:

The selectionchange event of the Selection API is fired when the selection object of the document is modified, or when the selection associated with an <input> or a <textarea> changes. The selectionchange event is fired at the document in the first case, on the element in the second case.

https://developer.mozilla.org/en-US/docs/Web/Events/selectionchange

Note that this is bleeding edge and not guaranteed to work across even major browsers.

There is "Text was selected" event. But only for textarea as I hava known.

<textarea onselect="message()" name="summary" cols="60" rows="5">
请写入个人简介,不少于200字!
</textarea>
var selectedText = "";


if (window.getSelection) {
selectedText = window.getSelection();
}


if (document.getSelection) {
selectedText = document.getSelection();
}


if (document.selection) {
selectedText = document.selection.createRange().text;
}


function textSelector() {
alert(selectedText);
}
textSelector();

I'm not sure about the mouse thing but this line works for mobile, this invoked every time a change made on the text selection -

document.addEventListener('selectionchange', () => {


});

When you press the mouse button down, the mousedown event is fired, when the mouse button is released, the mouseup and then click events are fired.

So we listen to the mouseup event and check if any text has been selected, and respective operations are performed.

const p = document.getElementById('interactiveText');


p.addEventListener('mouseup', (e) => {
const selection = window.getSelection().toString();


if (selection === '') {
console.log('click');
} else {
console.log('selection', selection);
}
});

There is a shortcut to get the selected text from event object.

event.currentTarget[event.currentTarget.selectedIndex].text

You can check it out on MDN. It's exactly what you need.

https://developer.mozilla.org/en-US/docs/Web/API/Element/select_event

The event is trigger and return the selected text when the selection is done.

If you want the selected text on every time the selection change. There is the selectionchange event for document and html input and textarea. Selectionchange event for document is supported on most browsers but it is supported only on Firefox for html input and textarea elements.

There is a polyfill for that which will support for all browsers.

https://github.com/channyeintun/selection