Usage:
<div id= "Test" >
<span style= "color:red" >test1</span> test2
</div>
In JS, you can use:
test.innerhtml:
That is, the entire contents of the object from its starting position to the terminating position, including the HTML tag.
The value of test.innerhtml in the example above is "<span style=" color:red ">test1</span> test2".
Test.innertext:
The content from the starting position to the terminating position, but it removes the HTML tag
The value of text.innertest in the example above is "Test1 test2", where the span tag is removed.
Test.outerhtml:
In addition to the entire contents of the innerHTML, the object label itself is included.
The value of text.outerhtml in the above example is <div id= "test" ><span style= "color:red" >test1</span> test2</div>
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)" >inerhtml content </a>
<a href= "Javascript:alert (test.outerhtml)" >outerhtml content </a>
Special Note:
innerHTML is a standard-compliant attribute, and innertext only works with Internet Explorer, so use innerHTML as much as possible, and less innertext, if you want to output content that does not contain HTML tags, You can use innerHTML to get content that contains HTML tags, and then use regular expressions to remove the HTML tags, and here's a simple example of the standard:
<a href= "Javascript:alert (document.getElementById (' Test '). Innerhtml.replace (/<.+?>/gim, '))" > No HTML, Meet the Standards </a>
Original link:
I'll check the original.
JavaScript InnerHTML, innertext, and outerhtml usage differences