A simple example of a JavaScript action Selection object _javascript tips

Source: Internet
Author: User

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.

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.