最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Which quotation marks to use and when? - Stack Overflow

programmeradmin9浏览0评论

for example here in documentation / it says

$('input[name*="man"]')

I would write instead

$("input[name*='man']")

is there any reason to use single or double quotation marks on inside or outside ?? is it just a matter of taste?

for example here in documentation http://api.jquery./attribute-contains-selector/ it says

$('input[name*="man"]')

I would write instead

$("input[name*='man']")

is there any reason to use single or double quotation marks on inside or outside ?? is it just a matter of taste?

Share Improve this question edited Jan 15, 2012 at 10:31 Felix Kling 817k181 gold badges1.1k silver badges1.2k bronze badges asked Jan 15, 2012 at 9:10 IAdapterIAdapter 64.9k73 gold badges186 silver badges243 bronze badges 4
  • 2 Duplicate of stackoverflow./questions/242813/… – Shai Mishali Commented Jan 15, 2012 at 9:12
  • 1 @ShaiMishali: Not a duplicate, as this also involves the syntax for jQuery selectors. – Guffa Commented Jan 15, 2012 at 9:39
  • @Guffa I don't really agree. jQuery is just a Javascript library and the "quotes" thing is the same for both. – Shai Mishali Commented Jan 15, 2012 at 14:52
  • 2 @ShaiMishali: Yes, jQuery is not a separate language, but the selectors used by jQuery is. It's not Javascript that is used in the selectors, but a syntax based on CSS selectors. The fact that Javascript syntax and selector syntax uses the same quotation marks has nothing to do with the fact that jQuery is a Javascript library. – Guffa Commented Jan 15, 2012 at 14:59
Add a ment  | 

2 Answers 2

Reset to default 5

It's just a matter of taste, and sometimes convenience.

You are using quotation marks both in Javascript and in a jQuery selector, and in both cases you can use either apostrophes (') or quotation marks (") to delimit strings.

In some cases it's more convenient to use one type over the other, for exampe when you have Javascript code in an HTML attribute, as apostropes doesn't need to be escaped:

<div onclick="alert('Hello world!');">

pared to:

<div onclick='alert(&quot;Hello world!&quot;)'>

Yes, it's a matter of taste. I generally use double quotes when the string literal contains single quotes, and vice-versa, to avoid having to escape them.

发布评论

评论列表(0)

  1. 暂无评论