Innerhtml usage in Javascript

Source: Internet
Author: User

<HTML>
<Head>
<Script language = "JavaScript">

Function Test (){
VaR STR = "";
STR + = "hello ,";
STR + = "this is a test! <Br/> ";
STR + = "I love you; <br/> ";
STR + = "I love you, too! ";
P. innerhtml = STR + "<br/>" + math. Random ();
SetTimeout ('test (); ', 1000 );
}
</SCRIPT>
</Head>
<Body onload = test ()>
<Span id = "p"> </span>
</Body>
</Html>
Differences between innertext and innerhtml:

Innerhtml comes with the syntax check function, which will automatically complete the incomplete HTML code. Run the following test code and you will find it.

Document. getelementbyid ("albumlist"). innerhtml = "<Table> <tr> ";
Alert (document. getelementbyid ("albumlist"). innerhtml );

He will automatically add the <tbody> and </tr> </table> tags in my code. Amazing !!!

Therefore, this is often used on webpages;

<Div id = "content"> </div>

<Script language = "JavaScript">

Document. getelementbyid ("content"). innerhtml = "content to be displayed"

</SCRIPT>

In this way, the "content to be displayed" will be displayed in the tag where ID is content ";

This can be used to display the summary of an article to prevent code in an article with HTML tags from being truncated from the end part of the tag,

E, G:

STR = "<a href = #/> Desert Oasis </a>

Left (STR, 60) is "the oasis in the desert <a href = #/> </a>

<Div id = "content"> </div>

<Script language = "JavaScript">

Document. getelementbyid ("content"). innerhtml = "Left (STR, 60 )"

</SCRIPT>

Innerhtml is to add HTML code to the block; innertext is to add text to the block. (Case Sensitive)

The innertext attribute can be used in IE browsers to obtain the text content after HTML tags is filtered out by the current element, which is useful in some cases. However, similar non-standard attributes/methods are not necessarily supported in other browsers.

Example: <HTML>
<Head>
<Title> DHTML example 13 </title>
<Style> <! --
Body {font-family: ""; color = "blue"; font-size: "9pt "}
. Blue {color: Blue; font-size: 9pt}
-->
</Style>
<Script language = "JavaScript">
Function outputtext ()
{
If(frm.txt. Text! = "")
{Output. innerhtml = "output text here: <u>" frm.txt. Value + "</u>" ;}// output is an object.
Else
{Output. innertext = "output text here :";}
} // Function

</SCRIPT>
</Head>

<Body>
<P> <br> </P>

<Form name = "frm">
<P> <font color = "gray"> Enter text in the text box: </font>
<Input type = "text" name = "TXT" size = "50"> <br>
<Input type = "button" value = "output text" name = "B1" class = "blue" onclick = "outputtext ()"> </P>
</Form>

<P id = "output"> output text here: </P>
</Body>
</Html>

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.