<HTML>
<Head>
<Title> query the selected text example </title>
<SCRIPT type = "text/JavaScript">
Function getselectedtext () {// obtain the selected text;
VaR result = "";
If (window. getselection ){
Result = Window. getselection (). tostring ();
Result + = "\ n _ window. getselection ()";
} Else if (document. getselection ){
Result = Document. getselection ();
Result + = "\ n _ document. getselection ()";
} Else if (document. Selection ){
Result = Document. selection. createRange (). text;
Result + = "\ n _ document. selection. createRange (). Text ";
}
If (window. addeventlistener ){
Console. Log (result );
} Else if (window. attachevent ){
Alert (result );
}
}
// Text input element Processing
Function gettextfieldselection (e ){
If (E. selectionstart! = Undefined & E. selectionend! = Undefined ){
VaR start = E. selectionstart;
Console. Log ("START _" + start );
VaR end = E. selectionend;
Console. Log ("End _" + end );
VaR Strout = E. value. substring (START, end );
Console. Log (Strout );
} Else {
Console. Log ("not supported on this browser .");
}
}
</SCRIPT>
</Head>
<Body>
<P> I love me, my wife! </P> <br>
<A href = "javascript:
VaR Q;
If (window. getselection ){
Q = Window. getselection (). tostring ();
} Else if (document. getselection ){
Q = Document. getselection ();
} Else if (document. Selection ){
Q = Document. selection. createRange (). text;
}
Void window. Open ('HTTP: // zh.wikipedia.org/wiki/' + q );
"> Search for the selected text in Wikipedia </a>
<Button onclick = "getselectedtext ()"> get selected text </button> <br>
<Label for = "textname"> check whether the text inside can be obtained: </label>
<Input type = "text" name = "textname" value = "I love you! "Size =" 23 "/>
<P> <B> compatibility Description: </B> If the selected text is in an input or textarea form element, the getselection () method of the window and document objects does not return it,
They can only return the selected text from the content of the document. However, the document. Selection attribute of IE returns the selected text from anywhere in the document. </P>
<Label for = "FILENAME"> processing of text input elements: </label>
<Input type = "text" name = "FILENAME" size = "23"/> <input type = "button" value = "element processing" onclick = "gettextfieldselection (Event) "/>
</Body>
</Html>