JQuery - 从HTML中选择元素时“”和“”之间的区别? [重复]

时间:2021-07-25 21:28:46

This question already has an answer here:

这个问题在这里已有答案:

I ve been learning JQuery, and everything was clear. Until I reached the strings. So far I knew that when you want to call something you do it this way $('callSomething'). Now Im learning how to move html elements around, and it was written to call something with double comment lines $("callSomething").

我一直在学习JQuery,一切都很清楚。直到我到达琴弦。到目前为止,我知道当你想要打电话时,你会这样做$('callSomething')。现在我正在学习如何移动html元素,并且它被编写为使用双重注释行调用$(“callSomething”)。

I started playing around the code, and I ve noticed that no matter what I will use ' ' or " " the code will work.

我开始玩代码,我注意到无论我将使用''或“”代码都可以工作。

     $('#one').after("<p>test</p>");
    var $p = $("p");
    $("#two").after($p)

Now I'm not sure what to use where. I understand that " " is used to call Strings. But I could use it to call elements too.

现在我不确定在哪里使用。我知道“”用于调用字符串。但我也可以用它来调用元素。

My question: Is there a specific reason when to use ' '? Or should I always use " " since its working and save myself the confusion?

我的问题:是否有特定原因何时使用''?或者我应该总是使用“”,因为它的工作,并保存自己的困惑?

Any explanation maybe on when to use ' ' and when '" "(if there is actual difference between them other than calling a string("< .p>test<./ p >")`?

任何解释可能是关于何时使用''和何时'“”(如果它们之间存在实际差异而不是调用字符串(“<。p> test <./ p>”))?

Thank you for your time

感谢您的时间

4 个解决方案

#1


This can help,

这可以帮助,

I wouldn't say there is a preferred method, you can use either. However If you are using one form of quote in the string, you might want to use the other as the literal.

我不想说有一个首选的方法,你可以使用其中之一。但是,如果您在字符串中使用一种形式的引用,则可能希望将另一种形式用作文字。

alert('Say "Hello"');
alert("Say 'Hello'");

The most likely reason is programmer preference / API consistency.

最可能的原因是程序员偏好/ API一致性。

When to use double or single quotes in JavaScript?

何时在JavaScript中使用双引号或单引号?

#2


There is no difference between the two versions of strings except this one:

除了这一个之外,两个版本的字符串没有区别:

Imagine you want to use a quotation mark inside a string:

想象一下,你想在字符串中使用引号:

"This is just a lil' test"

Or:

'"give them hell", he said'

You don't have to escape quotation marks in those two cases.

在这两种情况下,您不必转义引号。

#3


Both are equivalent, it's mainly a matter of preference and code legibility. If your string contains a single quote, you'll probably want to enclose it in double quotes and vice versa, so you don't have to escape your quotes, e.g.

两者都是等价的,主要是偏好和代码易读性。如果您的字符串包含单引号,您可能希望将其括在双引号中,反之亦然,因此您不必转义引号,例如

"It's nice" vs 'It\'s nice'

“这很好”vs“这很好”

or

'And she said "Oh my God"' vs "And she said \"Oh my God\""

“她说”噢,我的上帝“”vs“她说:”我的天啊“

You can find a more exhaustive answer to that question at https://*.com/a/242833/4604579

您可以在https://*.com/a/242833/4604579找到更详尽的答案。

#4


in javascript there is no difference. in other languages like c, ' will be used for characters and " will be used for character arrays aka strings.

在javascript中没有区别。在其他语言中,如c,'将用于字符,“将用于字符数组,即字符串。

#1


This can help,

这可以帮助,

I wouldn't say there is a preferred method, you can use either. However If you are using one form of quote in the string, you might want to use the other as the literal.

我不想说有一个首选的方法,你可以使用其中之一。但是,如果您在字符串中使用一种形式的引用,则可能希望将另一种形式用作文字。

alert('Say "Hello"');
alert("Say 'Hello'");

The most likely reason is programmer preference / API consistency.

最可能的原因是程序员偏好/ API一致性。

When to use double or single quotes in JavaScript?

何时在JavaScript中使用双引号或单引号?

#2


There is no difference between the two versions of strings except this one:

除了这一个之外,两个版本的字符串没有区别:

Imagine you want to use a quotation mark inside a string:

想象一下,你想在字符串中使用引号:

"This is just a lil' test"

Or:

'"give them hell", he said'

You don't have to escape quotation marks in those two cases.

在这两种情况下,您不必转义引号。

#3


Both are equivalent, it's mainly a matter of preference and code legibility. If your string contains a single quote, you'll probably want to enclose it in double quotes and vice versa, so you don't have to escape your quotes, e.g.

两者都是等价的,主要是偏好和代码易读性。如果您的字符串包含单引号,您可能希望将其括在双引号中,反之亦然,因此您不必转义引号,例如

"It's nice" vs 'It\'s nice'

“这很好”vs“这很好”

or

'And she said "Oh my God"' vs "And she said \"Oh my God\""

“她说”噢,我的上帝“”vs“她说:”我的天啊“

You can find a more exhaustive answer to that question at https://*.com/a/242833/4604579

您可以在https://*.com/a/242833/4604579找到更详尽的答案。

#4


in javascript there is no difference. in other languages like c, ' will be used for characters and " will be used for character arrays aka strings.

在javascript中没有区别。在其他语言中,如c,'将用于字符,“将用于字符数组,即字符串。