JS implements the function example of selecting the specified text content in the specified HTML Element Object,

Source: Internet
Author: User

JS implements the function example of selecting the specified text content in the specified HTML Element Object,

This example describes how to use JavaScript to specify the text content in the specified HTML Element Object. We will share this with you for your reference. The details are as follows:

This function is useful and can be used flexibly! The main functions are as follows:

// Specify part of the function selectSomeText (obj, start, end) {if (document. selection) {if (obj. tagName = 'textea ') {var I = obj. value. indexOf ("\ r", 0); while (I! =-1 & I <end) {end --; if (I <start) {start --;} I = obj. value. indexOf ("\ r", I + 1);} var range = obj. createTextRange (); range. collapse (true); range. moveStart ('character ', start); if (end! = Undefined) {range. moveEnd ('character ', end-start);} range. select ();} else {obj. selectionStart = start; var sel_end = end = undefined? Start: end; obj. selectionEnd = Math. min (sel_end, obj. value. length); obj. focus ();}}

And then, For example:

SelectSomeText (document. getElementById ('jiu _ textarea '), 2nd) // select to 8 words in jiu_textarea!

Complete example:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

Run the following command:

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.