Outhtml, innerhtml, innertext

Source: Internet
Author: User

Innerhtml, outerhtml, and innertext are all relative to the ID of a tag. The following code can be run directly to see the difference:

<Div id = "Div"> <input name = "button" value = "button" type = "button"> <font color = "green"> <H2> This Is div! </H2> </font> </div>
<Input name = "innerhtml" value = "innerhtml" type = "button" onclick = "alert (Div. innerhtml);">
<Input name = "outerhtml" value = "outerhtml" type = "button" onclick = "alert (Div. outerhtml);">
<Input name = "innertext" value = "innertext" type = "button" onclick = "alert (Div. innertext);">
<Input name = "outertext" value = "outertext" type = "button" onclick = "alert (Div. outertext);">


Innerhtml retrieves or sets the content in the tag;
Outerhtml retrieves or sets the content (including tags) of the entire tag ).

Innertext and outtext output the text information inside the tag
The above two text buttons output the same content:
However, when assigning values, outertext will be replaced with the target text with tags, while innertext will only replace the original text.

Use and difference of innerhtml and innertext in JS


<Div id = "test">
<Span style = "color: Red"> test1 </span> Test2

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. innertest 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>.

Complete example:

<Div id = "test">
<Span style = "color: Red"> test1 </span> Test2

<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>


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>

Bytes -------------------------------------------------------------------------------------------------------------------------------

<Head> <Frameset frameborder = "yes" frameborder = "1" rows = "40%, *">
<Frame name = "TOP" src = "1.html">
<Frame name = "bottom" src = "2.html">

<Script language = "JavaScript">
Function Init ()
VaR AAA = parent.w.fig 02.16.doc ument. Body. innerhtml;
Alert (AAA );
<P align = "center"> nothing </P>
<P align = "center"> <input type = "button" onclick = "Init ()"; value = "click"> </P>

<Center> automotive real estate women </center>
</Html> Test

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.