开发者

Difference between " and ' characters in JQuery [duplicate]

This question already has answers here: Closed 12 y开发者_如何学Goears ago.

Possible Duplicate:

When to Use Double or Single Quotes in JavaScript

I think $('#id') and $("#id") both is valid so " and ' character is samething and it is just preference to use ' instead of " ?


Single- and double-quotes do the same thing in JavaScript: they delimit string constants. It's convenient (though a little weird) to have both types of quotes available for the same purpose, because it makes quoting strings with embedded quotes a little easier sometimes. One example: jQuery selectors:

$('input[name="my input"]').val('');


jQuery is just a JavaScript library.

A string quoted with ' characters have have " characters inside it without them being escaped — and vice versa — that is all.


' and " may be used interchangeably in JavaScript/jQuery.

It's a little more robust to use '.

Why? Because it's easier to move JavaScript code between script tags and inline HTML elements - common XHTML syntax is to wrap event code in "" (eg onclick="alert('foo')"). Using single quotes prevents the need to escape (eg "alert(\"foo\")"), and allows you to easily move the code from the inline to an external script page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜