JavaScript Actions Select a simple instance of an object
Replace the selected text content, parameter text is the content to replace function Setselectiontext (text) {//Non IE browser if (window.getselection) {var sel = WINDOW.G
Etselection (); alert (Sel.rangecount);
The number of selections, usually 1. Sel.deletefromdocument (); Clears 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 selection of content var frag = selfrag.childnodes; If Deletefromdocument () is executed, this array length will be 0 for (var i = 0; i < frag.length; i++) {alert (frag[i].nodename); Enumeration of selected objects} var h1 = document.createelement (' H1 '); Generates an Insert object h1.innerhtml = text; Sets the content of this object R.insertnode (H1);
Inserting an object into a selection does not replace the selection, but is appended to the selection, so the deletefromdocument () function is executed before the normal paste substitution effect is required. else if (document.selection && document.selection.createRange) {//ie browser var sel = document.selection. Createrange (); Gets the Selection object alert (Sel.htmltext);
The HTML text for the Select area. Sel.pastehtml ('
Above this JavaScript operation to select the object of a simple example is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud habitat community.