I don't know how to write JS, but I will search for it. Here I found something written by someone else:
Copy codeThe Code is as follows: select (document, tanchu );
/* = Select [[
*
* Cross-browser selected text events
* @ Param
* Object o responds to the selected event's DOM object, required
* Function fn (sText, target, mouseP) callback function when the selected text is not empty, required
* |-@ Param
* |-SText selected text content
* |-Target element used to trigger the mouseup event
* |-Mouse coordinates when mouseP triggers the mouseup event
*/
Function select (o, fn ){
O. onmouseup = function (e ){
Var event = window. event | e;
Var target = event. srcElement? Event. srcElement: event.tar get;
If (/input | textarea/I. test (target. tagName) &/firefox/I. test (navigator. userAgent )){
// Select text in the text box of Firefox
Var staIndex = target. selectionStart;
Var endIndex = target. selectionEnd;
If (staIndex! = EndIndex ){
Var sText = target. value. substring (staIndex, endIndex );
Fn (sText, target );
}
}
Else {
// Obtain the selected text
Var sText = document. selection = undefined? Document. getSelection (). toString (): document. selection. createRange (). text;
If (sText! = ""){
// Pass the parameter to the callback function fn
Fn (sText, target );
}
}
}
}
/*] Select = */
Function tanchu (txt, tar ){
Alert ("the text belongs to the" + tar. tagName + "element, and the selected content is:" + txt );
}
Original Author see: http://momomolice.com/wordpress/archives/420.html
Appendix: only obtain the code of the selected text (the event should not be answered)Copy codeThe Code is as follows: function getSelectedText ()
{
If (window. getSelection)
{// This technique is the most likely to be standardized.
// GetSelection () returns a Selection object, which we do not document.
Return window. getSelection (). toString ();
}
Else if (document. getSelection)
{
// This is an older, simpler technique that returns a string
Return document. getSelection ();
}
Else if (document. selection)
{
// This is the IE-specific technique.
// We do not document the IE selection property or TextRange objects.
Return document. selection. createRange (). text;
}
}
After the function is run, the selected text is returned.
The original author is no longer available...