当前位置: 首页 > 图文教程 > 网络编程 > Javascript > JavaScript 特殊字符

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 JavaScript 特殊字符


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

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

插入特殊字符

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

请看下面的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 换页符