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>