The difference between InnerHTML outerhtml InnerText textcontent

Source: Internet
Author: User

Read a lot of blog on the Internet to introduce the difference between them, most of the innertext can not be used in the FF, but after their own experiment, it can be used, so he summed up a variety of situations.

The first is innerHTML, this is not much to say, return is the label inside all the content, including tags and text, as well as spaces

Give two chestnuts, compare the output difference caused by the space

<div id= "main" >1<span>123</span><p>222</p><div class= "test" >456</div> </div>

This removes the innerHTML in the tag with the ID main and, as a result, you can see that the spaces between the labels are also output.

<div id= "main" >1<span>123</span><p>222</p><div class= "test" >456</div> </div>

If the code is like this, the result is the bottom line.

So what is outerHTML? It actually contains the target tag, and the result is this.


OK, here's the difference between innertext and textcontent.

InnerText (compatible with all browsers here FF and Chrome are up to date)

<div id= "main" >1<div>123</div><p>222</p><p class= "test" >456</p>< span>123</span>    <span>222</span>            <span class= "test" >456</span></div >
Browser: Firefox browser: Chrome

On the left is the page display, the right is the console output of the innertext, you can see that the contents of the window and console output is basically the same, the space between the labels is also output.

But there's a difference between browsers, and Chrome has a blank line in the top of the first p tag compared to Firefox.

Ie9ie8

IE9 in the output and FF is the same, and IE8 and the following version only the effect of the line, see here is drunk.

Textcontent (IE8 and below are not available with the latest FF and Chrome)

Textcontent relative innertext is a lot simpler, it does not take into account the relationship between the labels, compared to the pure output text, it just to the text in the code between the space to output.

Or the same code?

<div id= "main" >1<div>123</div><p>222</p><p class= "test" >456</p>< span>123</span>  <span>222</span>           <span class= "test" >456</span></div >
Ff:

Chrome:

IE9 and above:

For Textcontent, there's no difference between browsers. Just the version of IE issue needs to be considered.


The difference between InnerHTML outerhtml InnerText textcontent

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.