JavaScript中的特殊字符

你可以在 JavaScript 中使用反斜杠来向文本字符串添加特殊字符。

反斜杠用来在文本字符串中插入省略号、换行符、引号和其他特殊字符。

如:

var txt="This is a "String" in this sentence.";

document.write(txt);

输出为:This is a

改进方法:

var txt="This is a \"String\" in this sentence.";

document.write(txt);

输出为:This is a "String" in this sentence.

常用特殊字符:\'、\"、\&、\\、\n、(换行符)\r、(回车符)\t、(制表符)\b、(退格符)\f(换页符)

JavaScript编码规则:

(1)JavaScript 对大小写敏感.当您创建或使用变量、对象及函数时,请注意字符的大小写。

(2)空格:JavaScript 会忽略多余的空格。所以您可以在代码中添加适当的空格,使得代码的可读性更强。

(3)换行:您可以在文本字符串内部使用反斜杠对代码进行折行。

比如:

document.write("Hello \

World!")

但是不能写成这样:

document.write \

("Hello World!")