Differences among innerHTML, outerHTML, innerText, and outerText and the insertAdjacentHTML () method, outerhtmloutertext

Source: Internet
Author: User

Differences among innerHTML, outerHTML, innerText, and outerText and the insertAdjacentHTML () method, outerhtmloutertext

When you need to insert a large number of new HTML tags to a document, it is very troublesome and inefficient to create a node and specify the relationship between them through multiple DOM operations, the insert TAG method is simpler and faster.

The insert tag has these four attributes.InnerHtml, outerHTML, innerText, outerText.

These four attributes have the acquired and set statuses.

Let's take a look at a short piece of code.

 

<Div> <p> I am a sub-element of div </p> </div>

 

* Here we need to get this div first, and I will use divnode instead. Test in chrome.

  • InnerHTMLObtain and set all HTML (including tags) in the call element ).
  • OuterHTMLObtain and set all HTML and its own (including tags) in the call element ).
  • InnerTextObtain and set the text in the call element.
  • OutTextSet (including tags) or get (excluding tags) The text of the object.

Let's take a look at the differences between the four statuses.

Divnode. innerHTML = "<I> I am the div sub-element I after innerHTML Settings </I>;

Divnode. outerHTML = "<B> <I> I am the B sub-element I after outerHTML Settings </I> </B>"

Divnode. innerText = "<I> I am the div sub-element I after innerHTML Settings </I> ";

Divnode. outerText = "<I> I am the div sub-element I after innerHTML Settings </I> ";

Insert as the previous peer element)

2. "afterBegin": insert to tag start (insert as the first child element)

3. "beforeEnd": insert to the end tag of the tag (insert as the last child element)

4. "afterEnd": insert to the end tag of the tag (insert as the last peer element)

 

Related Article

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.