Differences between innerHTML, innerText, and outerHTML

Source: Internet
Author: User

Usage:

<Div id = "test">
<Span style = "color: red"> test1 </span> test2
</Div>

In JS, you can use:

Test. innerHTML:

That is, all content from the starting position of the object to the ending position, including Html tags.

In the preceding example, the value of test. innerHTML is "<span style =" color: red "> test1 </span> test2 ".

Test. innerText:

Content from the starting position to the ending position, but it removes the Html Tag

In the preceding example, the value of text. innerTest is "test1 test2", in which the span tag is removed.

Test. outerHTML:

In addition to all the content of innerHTML, it also contains the object tag itself.

In the preceding example, the value of text. outerHTML is <div id = "test"> <span style = "color: red"> test1 </span> test2 </div>.


Complete example:

<Div id = "test">
<Span style = "color: red"> test1 </span> test2
</Div>

<A href = "javascript: alert (test. innerHTML)"> innerHTML content </a>
<A href = "javascript: alert (test. innerText)"> inerHTML content </a>
<A href = "javascript: alert (test. outerHTML)"> outerHTML content </a>

Note:

InnerHTML is a W3C standard attribute, while innerText is only applicable to IE browsers. Therefore, use innerHTML as much as possible, instead of innerText. If you want to output content without HTML tags, you can use innerHTML to obtain the content containing HTML tags and use regular expressions to remove HTML tags. The following is a simple W3C-compliant example:

<A href = "javascript: alert (document. getElementById ('test'). innerHTML. replace (/& lt;. ++?> /Gim, '')"> no html, compliant with W3C standards </a>

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.