开发者

Basic question about the use of quotation in jQuery

I heard somewhere that its better to write this:

$(document).ready(function() {
    $('a').append('<strong>Hello</strong>');
});

instead of this:

$(document).ready(function() {
    $("a").append("<strong>Hello</strong>");
});

But the last one, is the way that its written in the official documentation of jQuery.

So which gives more problems when the code gets larger? s开发者_运维问答ingle (') or double (") quotation? or its irrelevant?


In javascript, it is irrelevant. Both mean the same thing.

You may have heard the advice form languages like Perl where "" means interpolated string and '' means string literal. In which case '' incurs less processing overhead.

In javascript there are two ways to quote strings to allow simple shallow nesting of quotes:

div.innerHTML = '<a href="www.google.com">google</a>';
div.innerHTML = "<a href='www.google.com'>google</a>";


The only real advantage that I can see to using double quotes as a standard is if your strings contain apostrophes (single quotes), you don't have to change your style to accommodate them. Otherwise, javascript allows quoting with either single or double quotes.

Having said that, I tend to use single quotes for javascript out of a habit picked up long ago. Unfortunately, I'm not even that consistent about it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜