InnerHTML \ innerText \ outerHTML \ outerText

Source: Internet
Author: User
Document directory
  • InnerHtml usage
InnerHtml usage

<Html>
<Head>
<Script>
Function Test (){
Var str = "";
Str + = "Hello ,";
Str + = "This is a Test! <Br/> ";
Str + = "I Love you; <br/> ";
Str + = "I Love you, too! ";
P. innerHTML = str + "<br/>" + Math. random ();
SetTimeout ('test (); ', 1000 );
}
</Script>
</Head>
<Body onload = Test ();>
<Span id = "p"> </span>
</Doby>
</Html>

InnerHTML refers to all html content contained in the div, and innerText refers to the text content contained in the div and displayed on the page. outerHTML includes more div TAG content than innerHTML. There is no difference between outerText and innerText.
Differences between innerTEXT and innerHTML:

 

Use innerHTML with caution

Recently I am working on AJAX and want to use javascript to implement the DataList function. I encountered a difficult problem. Because the HTML to be generated is very long, I wrote it as follows:

List. innerHTML = "<table> <tr> ";

List. innerHTML + = "<td>"

......

However, the innerHTML generated in this way is always incorrect. After studying for a long time, it turns out that innerHTML is a ghost. innerHTML comes with the syntax check function, which will automatically complete the incomplete HTML code. run the following test code to find out.

Document. getElementById ("AlbumList"). innerHTML = "<table> <tr> ";
Alert (document. getElementById ("AlbumList"). innerHTML );

He will automatically add the <tbody> and </tr> </table> tags in my code. Amazing !!!

One way to prevent him from filling in automatically is to use an intermediate variable:

Var html = "<table> <tr> ";

Html + = "<td> ";

......

List. innerHTML = html;

In this way, the problem can be solved.

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.