<Div id = "test">
<Span style = "color: Red"> test1 </span> Test2
</Div>
In JS, you can use:
Test. innerhtml:
That is, all content from the starting position of the object to the ending position, including HTML tags.
In the preceding example, the value of test. innerhtml is "<span style =" color: Red "> test1 </span> Test2 ".
Test. innertext:
Content from the starting position to the ending position, but it removes the HTML Tag
In the preceding example, the value of text. innertext is "test1 Test2", in which the span tag is removed.
Test. outerhtml:
In addition to all the content of innerhtml, it also contains the object tag itself.
In the preceding example, the value of text. outerhtml is <Div id = "test"> <span style = "color: Red"> test1 </span> Test2 </div>.
Test. outertext:
Set (including tags) or get (excluding tags) The text of the object.
Complete example:
<Div id = "test">
<Span style = "color: Red"> test1 </span> Test2
</Div>
<A href = "javascript: Alert (test. innerhtml)"> innerhtml content </a>
<A href = "javascript: Alert (test. innertext)"> innertext content </a>
<A href = "javascript: Alert (test. outerhtml)"> outerhtml content </a>
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>