Home >Web Front-end >JS Tutorial >js skills--the wonderful use of the escape character ''_javascript skills

js skills--the wonderful use of the escape character ''_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:21:331158browse

// blueDestiny, never-online // blueDestiny [at] 126.com

Usually, when we dynamically give the innerHTML of a container, we usually do this:





It’s not troublesome if you get used to writing, but is there an easier way? Please see the example below:



Isn’t it so troublesome? But there are still some things to pay attention to. Look at the following example


You still have to use ""

'------------ ---------------------------------------------
' Principle:
'------------------------------------------------- --------
This is my own personal opinion. If there is something wrong, please point it out:
Let’s look at an example:


Output result:
s1: 2 s2: 2
That is to say, the escape character escapes the carriage return! In other words
Look at another example:


Output error, error message: unterminated string constant.
That is to say, it is because an extra space is added. Then try this again
<script> <BR>var div = document.getElementById("divc"); <BR>var html = "" <BR>html += "" <BR>+ "<h1>" <BR>+ "<a href='javascript:;' onclick=\"alert('javascript')\">DHTML innerHTML propery." <BR>+ ""; <BR>div.innerHTML = html; <BR></script> <script> <BR>var html='\ <BR><table width="100%" border="0" cellspacing="0" cellpadding="0">\ <BR><tr>\ <BR><td> \ <BR>\ <BR><tr>\ <BR><td> \ <BR>\ <BR>\ <BR>'; <BR>alert(html); <BR></script>The result is obvious. In the string, the "" escape character can escape the carriage return (that is, the carriage return character no longer exists), but it cannot Tab, and space character escapes (they exist, as the example above illustrates). <script> <BR>//要用\'把单引号转义 <BR>var html='\ <BR><h1>\ <BR>javascript技巧 <BR>\ <BR><a href="javascript:;" onclick="alert(\'javascript\')">javascript转义\ <BR><br/>\ <BR>power by \'blueDestiny, never-online\'\ <BR>'; <BR>alert(html); <BR></script><script> <BR>//s1和s2的字符a前都有一个空格 <BR>s1='\ <BR>a'; <BR>s2=' a'; <BR>document.write("s1: " + s1.length + "\ns2: " + s2.length); <BR></script>Finally, let me give you a little tip. Do you still remember the code above? <script> <BR>//下面这个字符串是有空格的,也就是这样s1='\ 的。 <BR>s1='\ <BR>a'; <BR>document.write("s1: " + s1.length); <BR></script><script> <BR>s1='\ \ <BR>a'; <BR>document.write("s1: " + s1.length); <BR></script> <script> <BR>//要用\'把单引号转义 <BR>var html='\ <BR><h1>\ <BR>javascript技巧 <BR>\ <BR><a href="javascript:;" onclick="alert(\'javascript\')">javascript转义\ <BR><br/>\ <BR>power by \'blueDestiny, never-online\'\ <BR>'; <BR>alert(html); <BR></script>Look carefully at the modal box that pops up and see what the string looks like? You should understand.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn