简体   繁体   English

JavaScript中“和”有什么区别?

[英]What is the difference between ' and " in JavaScript?

<button onClick="function("parameter")">Click</button>
<button onClick="function('parameter')">Click</button>

var variable = "value";
var variable = 'value';

Is there a difference? 有区别吗?

No. They are interchangeable by design. 不可以。它们可以通过设计互换。

The only requirement is that you need matching pairs (either use " or ' , but not both to signify a string). 唯一的要求是您需要匹配对(使用"' ,但不是两者都表示字符串)。

See the spec for string literals: 请参阅字符串文字的规范

StringLiteral:
          " StringCharactersDQopt " 
          ' StringCharactersSQopt '

When used within HTML, you need to be careful not to use the same delimiter in HTML attributes as the javascript ones (which is why your first example is not legal). 在HTML中使用时,您需要注意不要在HTML属性中使用与javascript相同的分隔符(这就是为什么您的第一个示例不合法)。

To function correctly you would need to change it to: 要正常运行,您需要将其更改为:

<button onClick='function("parameter")'>Click</button>

Yes, there's a difference when you mix it with HTML like you do: the first snippet will throw an exception because you need to escape the double quotes while the second will work (that's one of the reasons why you should avoid mixing markup with javascript). 是的,当你将它与HTML混合时会有区别:第一个片段会引发异常,因为你需要转义双引号,而第二个代码将起作用(这是你应该避免将标记与javascript混合的原因之一) 。 In pure javascript (a separate file) there's no difference. 在纯JavaScript(单独的文件)中,没有区别。

The two are equivalent as long as the same one is used at both the beginning and end of the string literal. 只要在字符串文字的开头和结尾都使用相同的两个,这两个就是等价的。 That said, choosing the correct one can avoid needless string escaping: 也就是说,选择正确的可以避免不必要的字符串转义:

<button onClick="function(&quot;parameter&quot;)">Click</button> <!-- becomes -->
<button onClick="function('parameter')">Click</button>

var foo = "And the computer said: \"Hello, world!\""; // becomes
var foo = 'And the computer said: "Hello, world!"';

This has a clear advantage when using JavaScript to generate HTML, as is common in scripts using jQuery. 这在使用JavaScript生成HTML时具有明显的优势,这在使用jQuery的脚本中很常见。

There is no difference. 没有区别。 ' and " are interchangeable. Now you can't have a string like this: var my_var = 'hello world" ; '"是可以互换的。现在你不能有这样的字符串: var my_var = 'hello world" ; Opening and close quotes have to match. 开盘价和收盘价必须匹配。 This does allow you to easily do: var my_variable = 'John says "I love JavaScript."' without having to escaping anything. 这确实可以让你轻松做到: var my_variable = 'John says "I love JavaScript."'而不必逃避任何事情。

so this: <button onClick="function("parameter")">Click</button> won't work because you have opened and closed the onclick event prematurely "function(" 所以这个: <button onClick="function("parameter")">Click</button>将无效,因为你已经过早地打开和关闭了onclick事件"function("

It is the same. 这是相同的。 The only reason for having ' and " is, that you cannot nest them, for example, in given onClick example -> 拥有'和'的唯一原因是,你无法嵌套它们,例如,在给定的onClick示例中 - >

onClick=" and there you need to use ' to encapsulate string "

The only time that there is a difference (that I am aware of) is in JSON: Double quotes are required around the keys and values. 唯一一次存在差异(我知道)是在JSON中:键和值周围需要双引号。

And yes, everyone is right; 是的,每个人都是对的; Your first line should read like this or it will throw an error: 你的第一行应该是这样读的,否则会引发错误:

<button onClick='function("parameter")'>Click</button>

I prefer to use the double quotes only (if possible) because I'm used to C like languages (C, C++, C#) where string can be wrapped only with double quotes. 我更喜欢只使用双引号(如果可能的话),因为我习惯于C语言(C,C ++,C#),其中字符串只能用双引号括起来。 (Single quotes is used, but to wrap char type) (使用单引号,但要包装char类型)

To answer the question itself: same like all others said, no real difference - guess it exists to support cases when you mix JavaScript with HTML. 回答问题本身:就像所有其他人所说的一样,没有真正的区别 - 猜测当你将JavaScript与HTML混合时,它是支持案例的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM