< Div ID = "Divc" />
< Script Language = "JavaScript" >
VaR Div = Document. getelementbyid ( " Divc " );
VaR Html = ""
Html + = ""
+ " <H1> "
+ " <A href = 'javascript:; 'onclick = \ " Alert ( ' Javascript ' )\ " > DHTML innerhtml propery. </a> "
+ " </H1> " ;
Div. innerhtml = HTML;
</ Script >
If you get used to writing it, it is not troublesome. But is there a simpler method? See the following example:
< Script Language = "JavaScript" >
VaR Html = ' \
<Table width = "100%" border = "0" cellspacing = "0" cellpadding = "0"> \
<Tr> \
<TD> & nbsp; </TD> \
</Tr> \
<Tr> \
<TD> & nbsp; </TD> \
</Tr> \
</Table> \
' ;
Alert (HTML );
</ Script >
Isn't it so troublesome? But pay attention to the following example.
< Script Language = "JavaScript" >
// Escape single quotes \'
VaR Html = ' \
<H1> \
Javascript skills \
</H1> \
<A href = "javascript:;" onclick = "alert (\ ' Javascript \ ' ) "> JavaScript escape </a> </font> \
<Br/> \
Power \ ' Bluedestiny, never - Online \ ' \
' ;
Alert (HTML );
</ Script >
"\" Must be used for the escape "\"
'-------------------------------------------------------
'Principle:
'-------------------------------------------------------
This is my personal opinion. If there is something wrong, please point out:
Let's take a look at the example:
< Script Language = "JavaScript" >
// There is a space before characters a of S1 and S2.
S1 = ' \
A ' ;
S2 = ' A ' ;
Document. Write ( " S1: " + S1.length + " \ NS2: " + S2.length );
</ Script >
Output result:
S1: 2 S2: 2
That is to say, the escape character escapes the carriage return! That is to say
Let's look at another example:
< Script Language = "JavaScript" >
// The following string contains spaces, that is, S1 =.
S1 = ' \
A ' ;
Document. Write ( " S1: " + S1.length );
</ Script >
Output Error. error message: the String constant is not ended.
That is to say, it is because a space is added. Try again.
< Script Language = "JavaScript" >
S1 = ' \\
A ' ;
Document. Write ( " S1: " + S1.length );
</ Script >
The result is obvious. In the string, the "\" Escape Character can escape the carriage return (that is, the carriage return character does not exist), but the Tab character cannot be used, and space characters escape (they exist, as described in the above example ).
Finally, let's give you a small tips. Do you still remember the Code above?
script language = "JavaScript" >
// escape single quotes with \ '
var HTML = ' \< br>
\< br> JavaScript skills \
\< br>
JavaScript escape \
\
power by \ ' bluedestiny, never - online \ ' \< br> ' ;
alert (HTML );
script