outerHTML, innerHTML and innertext differences in JavaScript

Source: Internet
Author: User
Tags visibility

Sample code:

The code is as follows Copy Code

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

In JS can be used:

The code is as follows Copy Code

document.getElementById (' Test '). InnerHTML:

That is, everything from the start position of the test object to the ending position, including its internal HTML tag.

The return value in the previous example: <span style= "color:red" >test1</span> test2

The code is as follows Copy Code


document.getElementById (' Test '). innertext:

(Note: InnerText only applies to IE browser, so try not to use innertext)

The content from the start position to the ending position, but it removes the HTML tag.

The return value in the previous example: Test1 test2, where the span label was removed.

The code is as follows Copy Code
document.getElementById (' Test '). outerHTML:

In addition to the full contents of the innerHTML, the matching object label itself is included.

The return value in the previous example: <div id= "test" ><span style= "color:red" >test1</span> test2</div>


Firefox does not support JS innerHTML


The original code is as follows:

The code is as follows Copy Code
<span id= "Ad_1" ></span>
<span id= "Ad_11" style= "Visibility:hidden;" >
<script language= "JavaScript" src= "/js/ad_1.js" ></script>
</SPAN>
<script>ad_1.innerhtml=ad_11.innerhtml;ad_11.innerhtml= "";</script>

The above code in IE, Chrome and other browsers can be the correct execution of the replacement, but under Firefox can not. In fact, the solution is also very simple, let us follow the ECMAScript specification to write code. The code above is fixed to the following:

The code is as follows Copy Code

<span id= "Ad_1" ></span>
<span id= "Ad_11" style= "Visibility:hidden;" >
<script language= "JavaScript" src= "/js/ad_1.js" ></script>
</SPAN>
<script>document.getelementbyid ("Ad_1"). Innerhtml=document.getelementbyid ("Ad_11"). InnerHTML; document.getElementById ("Ad_11"). Innerhtml= "";</script>

Instead of using div or span IDs directly, you can use document.getElementById ("div_id") to perform the substitution. In addition, Firefox does not support the innertext attribute.

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.