SoFunction
Updated on 2025-02-28

Simple instance of JavaScript operation selection object

Simple instance of JavaScript operation selection object

//Replace the selected text content, the parameter text is the content to be replacedfunction SetSelectionText(text) {
  //Not IE browser  if () {
    var sel = ();
    alert(); //The number of selections is usually 1.    (); //Clear the selected content    var r = (0); //Even if deleteFromDocument() has been executed, this function still returns a valid object.    var selFrag = (); //Clone the selected content    var frag = ; //If deleteFromDocument() is executed, the length of this array will be 0    for (var i = 0; i < ; i++) {
      alert(frag[i].nodeName); //Enum selected objects    }
    var h1 = ('H1'); //Generate an insert object     = text; //Set the content of this object    (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 you need a normal paste replacement effect, execute the deleteFromDocument() function before.  }
  else if ( && ) {
    //IE browser    var sel = (); //Get the selection object    alert(); //The html text of the selection area.    ('<h1>Title</h1>'); //Paste the html content into the selection area and replace the selected content.  }
}

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 me more.