JavaScript 字符串中的双引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10055773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:39:26  来源:igfitidea点击:

Double quote in JavaScript string

javascripthtml

提问by user1318393

How can I include a double quote in a JavaScript string to be shown in the browser?

如何在浏览器中显示的 JavaScript 字符串中包含双引号?

I am working on my JavaScript homework and I have to include double quotes in the middle of my list as shown below:

我正在做我的 JavaScript 作业,我必须在列表中间包含双引号,如下所示:

if (i == 0) {

   error +=  "<li> this is not the name "....." </li>\n"
}

回答by Alex

Use single quotes.

使用单引号。

error +=  '<li> this is not the name "....." </li>\n';

Or escape the double quotes.

或者转义双引号。

error +=  "<li> this is not the name \".....\" </li>\n";

回答by Oded

Use single quotes as your string delimiters:

使用单引号作为字符串分隔符:

if (i == 0) {
   error += '<li> this is not the name "....." </li>\n'
}

If you have single quotes in the string, delimit it with double quotes.

如果字符串中有单引号,请用双引号分隔。

If you have double quotes in the string, delimit it with single quotes.

如果字符串中有双引号,请用单引号分隔。

If you need to use both types in the string, escape whatever delimiter you have chosen by prefixing it with a \.

如果您需要在字符串中同时使用这两种类型,请通过在它前面加上前缀来转义您选择的任何分隔符\

回答by stefan bachert

Consider to use the default entity

考虑使用默认实体

 &quot;

回答by Jukka K. Korpela

error +=  "<li> this is not the name “.....” </li>\n"

Or, if you use a variant of English where single quotation marks are the norm, as usual in British English,

或者,如果您使用单引号是标准的英语变体,就像英式英语一样,

error +=  "<li> this is not the name ‘.....' </li>\n"

Using proper punctuation marks, you won't encounter problems with the quoting conventions of JavaScript. Computer languages use ASCIIquotes, human languages don't.

使用正确的标点符号,您不会遇到 JavaScript 引用约定的问题。计算机语言使用ASCII引号,人类语言不使用。

Just make sure your document is properly encoded (both Windows-1252and UTF-8would do here) and the encoding is properly declared.

只需确保您的文档正确编码(Windows-1252UTF-8都可以在这里执行)并且正确声明编码。