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.