// BlueDestiny, never-online // blueDestiny [at] 126.com
Generally, when we dynamically specify the innerHTML of a iner, we usually do the following:
<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> </td> \
</Tr> \
<Tr> \
<Td> </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 by \ '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 the s1 and s2 characters
S1 = '\
A ';
S2 = 'a ';
Document. write ("s1:" + s1.length + "\ ns:" + 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 above Code?
<Script language = "JavaScript">
// Escape single quotes \'
Var html = '\
<H1> \
Javascript skills \
</H1> \
<A href = "javascript:;" onclick = "alert (\ 'javascript \ ')"> javascript escape </a> </font> \
<Br/> \
Power by \ 'bluedestiny, never-online \'\
';
Alert (html );
</SCRIPT>
Take a closer look at the pop-up modal box and see what the string looks like? It should be clear.