Simple Example of an object selected for JavaScript operations, javascript instance

Source: Internet
Author: User

Simple Example of an object selected for JavaScript operations, javascript instance

Simple Example of an object selected by a JavaScript operation

// Replace the selected text content. The parameter text is the content to replace function SetSelectionText (text) {// non-IE browser if (window. getSelection) {var sel = window. getSelection (); alert (sel. rangeCount); // The number of constituency, usually 1. sel. deleteFromDocument (); // clear the selected content var r = sel. getRangeAt (0); // This function returns a valid object even if deleteFromDocument () has been executed. var selFrag = r. cloneContents (); // clone the selected content var frag = selFrag. childNodes; // If deleteFromDocument () is executed, the array length will be 0 for (var I = 0; I <frag. length; I ++) {alert (frag [I]. nodeName); // enumeration selected object} var h1 = document. createElement ('h1 '); // generate an insert object h1.innerHTML = text; // set the content of this object r. insertNode (h1); // Insert the object to the constituency. This operation does not replace the selected content, but appends the object to the end of the constituency, previously executed deleteFromDocument () function .} else if (document. selection & document. selection. createRange) {// IE browser var sel = document. selection. createRange (); // obtain the selected area object alert(sel.html Text); // select the html Text of the area. sel. pasteHTML ('

The simple example of the above JavaScript operation selection object is all the content shared by the editor. I hope to give you a reference, and I hope you can also support the help house.

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.