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