Javascript _ query the selected text example;

Source: Internet
Author: User

<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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.