js中怎么表示转义符

JavaScript021

js中怎么表示转义符,第1张

js中的特殊字符,加上转义符\ 。

例如:

var txt="We are the so-called "Vikings" from the north." document.write(txt) 【错误】

var txt="We are the so-called \"Vikings\" from the north." document.write(txt) 【正确】

JScript 提供了一些特殊字符,允许在字符串中包括一些无法直接键入的字符。每个字符都以反斜杠开始。反斜杠是一个转义字符,表示 JScript 解释器下面的字符为特殊字符。

转义序列 字符

\b 退格

\f 走纸换页

\n 换行

\r 回车

\t 横向跳格 (Ctrl-I)

\' 单引号

\" 双引号

\\ 反斜杠

请注意,由于反斜杠本身用作转义符,因此不能直接在脚本中键入一个反斜杠。如果要产生一个反斜杠,必须一起键入两个反斜杠 (\\)。

document.write('The image path is C:\\webstuff\\mypage\\gifs\\garden.gif.')

document.write('The caption reads, "After the snow of \'97. Grandma\'s house is covered."')