Differences between javascript innerHTML, outerHTML, innerText, and outerText

Source: Internet
Author: User

1. Function Description:
InnerHTML sets or obtains the HTML in the start and end tags of an object.
OuterHTML sets or obtains the HTML format of the object and its content
InnerText sets or obtains the text in the object's start and end labels.
OuterText setting (including tags) or obtaining (excluding tags) the text of an object
2. Example
<Html>
<Head>
<Title> Demo </title>
<Style> <! --
Body {font-family: ""; color = "blue"; font-size = "9pt "}
--> </Style>
<Script language = "JavaScript">
//. InnerHTML
Function innerHTMLDemo ()
{
Test_id1.innerHTML = "<I> <u> set or retrieve HTML in the start and end tags of an object. </u> </I> ";
}
//. InnerText
Function innerTextDemo ()
{
Test_id2.innerText = "<I> <u> sets or obtains the text in the start and end labels of an object. </u> </I> ";
}
//. OuterHTML
Function outerHTMLDemo ()
{
Test_id3.outerHTML = "<I> <u> sets or obtains the HTML format of the object and its content. </u> </I> ";
}
//. OuterText
Function outerTextDemo ()
{
Test_id4.outerText = "<I> <u> set (including tags) or get (excluding tags) the text of an object. </u> </I> ";
}
</Script>
</Head>
<Body>
<Ul>
<Li id = "test_id1" onclick = "innerHTMLDemo ()"> innerHTML effect. </li>
<Li id = "test_id2" onclick = "innerTextDemo ()"> innerText effect. </li>
<Li id = "test_id3" onclick = "outerHTMLDemo ()"> outerHTML effect. </li>
<Li id = "test_id4" onclick = "outerTextDemo ()"> outerText effect. </li>
</Ul>
</Body>
</Html>
3. Differences:
To put it simply, the differences between innerHTML, outerHTML, innerText, and outerText are as follows:
1) innerHTML and outerHTML will be parsed when setting the object content, while innerText and outerText will not.
2) When setting, innerHTML and innerText only set the text in the tag, while outerHTML and outerText set the text including the tag.

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 (/<. ++?> /Gim, '')"> no html, compliant with W3C standards </a>

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.