换行
您可以在文本字符串内部使用反斜杠对代码进行折行。下面的例子是正确的:
document.write("hello \ world!")
但是不能像这样折行:
document.write \ ("hello world!")
插入特殊字符
反斜杠用来在文本字符串中插入省略号、换行符、引号和其他特殊字符。
请看下面的 javascript 代码:
var txt="we are the so-called "vikings" from the north." document.write(txt)
在 javascript 中,字符串使用单引号或者双引号来起始或者结束。这意味着上面的字符串将被截为:we are the so-called。
要解决这个问题,就必须把在 "viking" 中的引号前面加上反斜杠 (\)。这样就可以把每个双引号转换为字面上的字符串。
var txt="we are the so-called \"vikings\" from the north." document.write(txt)
现在 javascript 就可以输出正确的文本字符串了:we are the so-called "vikings" from the north。
这是另一个例子:
document.write ("you \& me are singing!")
上面的例子会产生以下输出:
you & me are singing!
下面的表格列出了其余的特殊字符,这些特殊字符都可以使用反斜杠来添加到文本字符串中:
代码
输出
\'
单引号
\"
双引号
\&
和号
\\
反斜杠
\n
换行符
\r
回车符
\t
制表符
\b
退格符
\f
换页符
======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/