innerHTML and innertext distinguish

Source: Internet
Author: User

Example code:
<div id= "Test" >
<span style= "color:red" >test1</span> test2

Href= "Javascript:alert (test.innerhtml)" >innerhtml content </a>
Href= "Javascript:alert (test.innertext)" >inerhtml content </a>

Common denominator: innerHTML and innertext will replace the contents of the element.
Different points:
That is, the entire contents of the object from its starting position to the terminating position, including the HTML tag.

The value of test.innerhtml in the example above is "<span style=" color:red ">test1</span>
Test2 ".
The content from the starting position to the terminating position, but it removes the HTML tag

The value of text.innertest in the example above is "Test1 test2", where the span tag is removed.

It is worth noting that innerHTML is a standard-compliant attribute, and innertext only works with IE, so use innerhtml as much as possible, and less innertext, if you want to output content that does not contain HTML tags, You can use innerHTML to get content that contains HTML tags, and then use regular expressions to remove HTML tags.

innerHTML and innertext distinguish

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: 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.