Innerhtml,outerhtml,innertext,outertext difference and insertadjacenthtml () method

Source: Internet
Author: User

When you need to insert a large number of new HTML tags into a document, it can be cumbersome and inefficient to create a node and specify the relationship between them by multiple DOM operations, and the method of inserting markup is simpler and faster.

The insert tag has these four properties innerHtml,outerhtml,innertext,outertext.

These four properties have both a get state and a set state.

Let's look at a small piece of code first

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

* Here we have to get this div first, I use Divnode instead. Test it under Chrome browser.

    • InnerHTML gets and sets all the HTML (including tags) inside the calling element.
    • outerhtml gets and sets all the HTML and itself (including tags) inside the calling element.
    • InnerText Gets and sets the text inside the calling element.
    • Outtext sets (including tags) or gets the text of the object (excluding the label).

Let's take a look at the four differences in the get state.

When setting state

Divnode.innerhtml= "<i> i am innerHTML set div sub-element i</i> ;

Divnode.outerhtml= "<b><i> I am outerhtml set B sub-element I </i></b>"

divnode.innertext= "<i> i am innerhtml set div sub-element i</i> ";

divnode.outertext= "<i> i am innerhtml set div sub-element i</ I >";

These are the four attributes and differences of the insertion mark, and a insertadjacenthtml () method to insert the HTML content. insertAdjacentText () is similar to inserting text.

Insertadajcenthtml (Where,htmltext)

Parameters:

Where: Specify where to insert the HTML tag statement

HTMLText: HTML text to insert

The first parameter must be one of the following values:

1. "Beforebegin": Insert before label start ( Insert as previous sibling element)

2. "Afterbegin": inserted after the tag start tag (inserted as the first child element)

3. "BeforeEnd": inserted before the tag end tag (inserted as the last child element)

4. "Afterend": inserted after the tag end tag (inserted as the latter sibling element)

Innerhtml,outerhtml,innertext,outertext difference and insertadjacenthtml () method

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.