Simple instance of JavaScript operation selection object
//Replace the selected text content, the parameter text is the content to be replaced function SetSelectionText(text) { //Not IE browser if (window.getSelection) { var sel = window.getSelection(); alert(sel.rangeCount); //The number of selections is usually 1. sel.deleteFromDocument(); //Clone the selected content var r = sel.getRangeAt(0); //Even if deleteFromDocument() has been executed, this function still returns a valid object. var selFrag = r.cloneContents(); //Clone the selected content var frag = selFrag.childNodes; //If deleteFromDocument() is executed, the length of the array will be 0 for (var i = 0; i < fragment.length; i++) { alert(frag[i].nodeName); //Enum the 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 into the selection, this operation will not replace the selected content, but will be appended to the back of the selection. Therefore, if a normal paste replacement effect is required, execute the deleteFromDocument() function before. } else if (document.selection && document.selection.createRange) { //IE browser var sel = document.selection.createRange(); //Get the selection object alert(sel.htmlText); //The html text of the selection area. sel.pasteHTML('<h1>Title</h1>'); //Paste in the html content of the selection area, and the selected content will be replaced. }}The above simple example of JavaScript operation selection object is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.