当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JS 特殊字符

JavaScript
JS数学对象参考
JS字符串对象参考
JS函数参考
JS事件参考
Javascript 常用正则表达式
FF和IE下的js兼容性问题
jQuery 简单介绍
jQuery / 核心 / $(expression, [context] ) 函数
jQuery / 核心 / $(html) 函数
如何使用JS来判断浏览器类型(ie、firefox,等等)
Javascript在IE和FireFox中的不同表现
3个js字符编码函数区别
javascript 中的 XMLDOM 对象

JavaScript 中的 JS 特殊字符


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1063 ::
收藏到网摘: n/a

In JavaScript you can add special characters to a text string by using the backslash sign.
使用backslash( \ 反斜杠)标记你可以在JS中添加特殊文字。


Insert Special Characters
插入特殊文字

The backslash () is used to insert apostrophes, new lines, quotes, and other special characters into a text string.
(\ 反斜杠)被用来插入省略符号,新的行,引用和其他插入在字符串中的特殊文字。

Look at the following JavaScript code:
请看下面的JS代码:

var txt="We are the so-called "Vikings" from the north."

document.write(txt)

In JavaScript, a string is started and stopped with either single or double quotes. This means that the string above will be chopped to: We are the so-called
在JS中,一字符串可以用单引号或是双引号来开始和结束。这就意味着上面的字符串将被切成:We are the so-called

To solve this problem, you must place a backslash (\) before each double quote in "Viking". This turns each double quote into a string literal:
要解决这个问题,你必须在"Viking"的每个双引号前放置一个\ (反斜杠)。这样就能在字符串中正确返回每个双引号了:

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

JavasScript will now output the proper text string: We are the so-called "Vikings" from the north.
现在JS就能输出合适的文字了: We are the so-called "Vikings" from the north.

Here is another example:
这是另外一个例子:

document.write ("You & me are singing!") 

The example above will produce the following output:
上面的例子会输出下面的内容:

You & me are singing!

The table below lists other special characters that can be added to a text string with the backslash sign:
下面的表格罗列了其他一些特殊文字:

Code
代码
Outputs
输出结果
\' single quote
单引号
\" double quote
双引号
\& ampersand
&符号
\\ backslash
反斜杠
\n new line
换行符
\r carriage return
回车符
\t tab
制表符
\b backspace
退格符
\f form feed
分页符

评论 (0) All

登陆 | 还没注册?