我如何在JavaScript字符串中包含双引号以在浏览器中显示?
我正在做我的JavaScript作业,我必须在我的列表中间包含双引号,如下所示:
if (i == 0) {
error += "<li> this is not the name "....." </li>\n"
}
我如何在JavaScript字符串中包含双引号以在浏览器中显示?
我正在做我的JavaScript作业,我必须在我的列表中间包含双引号,如下所示:
if (i == 0) {
error += "<li> this is not the name "....." </li>\n"
}
使用单引号。
error += '<li> this is not the name "....." </li>\n';
或者转义双引号。
error += "<li> this is not the name \".....\" </li>\n";
使用单引号作为字符串的定界符:
if (i == 0) {
error += '<li> this is not the name "....." </li>\n'
}
如果字符串中有单引号,请用双引号括起来。
如果字符串中有双引号,请用单引号括起来。
如果需要在字符串中使用两种引号,请在所选定的分隔符前加上\
进行转义。
"
error += "<li> this is not the name “.....” </li>\n"
或者,如果你使用的是单引号是惯用法的英语变体,就像英式英语一样,
error += "<li> this is not the name ‘.....’ </li>\n"
使用适当的标点符号,您将不会遇到JavaScript引用约定的问题。计算机语言使用ASCII引号,而人类语言则不使用。
只需确保您的文档已正确编码(这里可以使用Windows-1252和UTF-8),并且已正确声明编码方式。