双引号与JavaScript中的单引号[重复]

时间:2022-10-28 15:43:24

Possible Duplicate:
When to Use Double or Single Quotes in JavaScript
Are there differences between ' and "

可能重复:何时在JavaScript中使用双引号或单引号'和'之间存在差异

I am wondering if there is a difference between using single quotes vs double quotes in JavaScript (JQuery selectors etc.).

我想知道在JavaScript(JQuery选择器等)中使用单引号与双引号之间是否存在差异。

Both seem to work just fine, so is there a difference?

两者似乎工作得很好,所以有区别吗?

4 个解决方案

#1


51  

The difference is that you don't need to escape single quotes in double quotes, or double quotes in single quotes. That is the only difference, if you do not count the fact that you must hold the Shift key to type ".

区别在于您不需要使用双引号中的单引号或单引号中的双引号来转义。这是唯一的区别,如果你不计算你必须按住Shift键输入“。

#2


18  

It doesn't matter unless you're trying to write JSON, in which case you must use double quotes for object literals.

除非您尝试编写JSON,否则无关紧要,在这种情况下,您必须对对象文字使用双引号。

#3


10  

There is no special difference between single and double quotes (like it is in PHP).

单引号和双引号之间没有特别的区别(就像在PHP中一样)。

The only actual difference is that you can write a double quote " in a single-quoted string without escaping it, and vice versa.

唯一的实际区别是,您可以在单引号字符串中编写双引号而不转义它,反之亦然。

So, if you are going to output some HTML in your JavaScript, like this:

那么,如果要在JavaScript中输出一些HTML,如下所示:

<div id = "my_div"></div>

single quotes are more useful :-)

单引号更有用:-)

#4


2  

They work the same, but they must match. You cannot start a string with a single and end with a double, or the opposite. Other than that, they are interchangeable.

它们的工作方式相同,但必须匹配。你不能用一个单词开始一个字符串,以一个双尾结束,或者相反。除此之外,它们是可以互换的。

#1


51  

The difference is that you don't need to escape single quotes in double quotes, or double quotes in single quotes. That is the only difference, if you do not count the fact that you must hold the Shift key to type ".

区别在于您不需要使用双引号中的单引号或单引号中的双引号来转义。这是唯一的区别,如果你不计算你必须按住Shift键输入“。

#2


18  

It doesn't matter unless you're trying to write JSON, in which case you must use double quotes for object literals.

除非您尝试编写JSON,否则无关紧要,在这种情况下,您必须对对象文字使用双引号。

#3


10  

There is no special difference between single and double quotes (like it is in PHP).

单引号和双引号之间没有特别的区别(就像在PHP中一样)。

The only actual difference is that you can write a double quote " in a single-quoted string without escaping it, and vice versa.

唯一的实际区别是,您可以在单引号字符串中编写双引号而不转义它,反之亦然。

So, if you are going to output some HTML in your JavaScript, like this:

那么,如果要在JavaScript中输出一些HTML,如下所示:

<div id = "my_div"></div>

single quotes are more useful :-)

单引号更有用:-)

#4


2  

They work the same, but they must match. You cannot start a string with a single and end with a double, or the opposite. Other than that, they are interchangeable.

它们的工作方式相同,但必须匹配。你不能用一个单词开始一个字符串,以一个双尾结束,或者相反。除此之外,它们是可以互换的。