I do not how to write JS, but will search, here found some other people write good things:
Copy Code code as follows:
Select (document, Tanchu);
/*=select[[
*
* Cross-Browser Select text events
* @param
* Object o responds to the DOM object of the selected event, required
* function fn (STEXT,TARGET,MOUSEP) to check for callback functions when text is not empty required
* |-@param
* |-stext selected text content
* |-target the element that triggers the MouseUp event
* |-MOUSEP mouse coordinates when triggering MouseUp event
*/
function Select (o, FN) {
O.onmouseup = function (e) {
var event = Window.event | | E
var target = event.srcelement? Event.srcElement:event.target;
if (/input|textarea/i.test (target.tagname) &&/firefox/i.test (navigator.useragent)) {
Firefox selects text in a text box
var Staindex=target.selectionstart;
var endindex=target.selectionend;
if (Staindex!=endindex) {
var stext=target.value.substring (Staindex,endindex);
FN (Stext,target);
}
}
else{
Get selected text
var stext = Document.selection = = undefined? Document.getselection (). toString ():d ocument.selection.createRange (). text;
if (stext!= "") {
Passing parameters to the callback function fn
FN (stext, target);
}
}
}
}
/*]]select=*/
function Tanchu (Txt,tar) {
Alert ("The text belongs to the +tar.tagname+ element, and the selection is:" +txt);
}
See the original author: http://momomolice.com/wordpress/archives/420.html
Attach: Code that only gets the selected text (does not respond to the event)
Copy Code code as follows:
function Getselectedtext ()
{
if (window.getselection)
{//This technique are 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 the older, simpler technique that returns a string
return Document.getselection ();
}
else if (document.selection)
{
This is the ie-specific technique.
We Don't document the IE selection property or TextRange objects.
Return Document.selection.createRange (). text;
}
}
The selected text is returned when the function is run.
The original author can not test ...