javascript jQuery - 连接带引号的字符串的更好方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3463439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:15:06  来源:igfitidea点击:

jQuery - better way to concatenate strings that have quotations?

javascriptjquerystring

提问by ina

Is there a better way to concatenate strings that have "'s (such as HTML tag attribute definitions) in jQuery than escaping the quote?

有没有比转义引号更好的方法来连接 jQuery 中具有 " 的字符串(例如 HTML 标记属性定义)?

Escapy Example:

逃逸示例:

$(this).prepend("<label for=\""+$(this).attr("id")+"\">"+"a"+"</label>");

$(this).prepend("<label for=\""+$(this).attr("id")+"\">"+"a"+"</label>");

回答by Nick Craver

You can use the object method of creation ($(html, props)), like this:

您可以使用创建对象的方法 ( $(html, props)),如下所示:

$('<label />', { for: this.id, text: 'a' }).prependTo(this);
//or:
$(this).prepend($('<label />', { for: this.id, text: 'a' }));

This has the advantage of calling .text()internally, taking care of any encoding issues. Also, if you're doing this a lot, it'll be faster, since the HTML fragment is consistent and cached, so a clone of the node is used, rather than turning a string into a document fragment each time. The more elements like this you're creating, the faster it is over the string method. For the same caching reasons, the bigger the element (up to a 512 char string) the bigger the gains.

这具有.text()内部调用的优点,可以处理任何编码问题。另外,如果你这样做一个很多,这将是更快,因为HTML片段是一致的缓存,因此节点的克隆使用,而不是每次把一个字符串转换成文档片段。您创建的此类元素越多,它在 string 方法上的速度就越快。出于相同的缓存原因,元素越大(最多 512 个字符的字符串),收益就越大。

回答by Jeff Treuting

you can use single quotes for the main string and then you don't have to escape the double quote.

您可以对主字符串使用单引号,然后您不必转义双引号。

$(this).prepend('<label for="'+$(this).attr("id")+'">'+ a +'</label>');

回答by Ken Redler

This is why I primarily use single quotes $('here'). Less to worry about when you're working with HTML that inevitably is full of double quotes.

这就是我主要使用单引号的原因$('here')。当您使用不可避免地充满双引号的 HTML 时,无需担心。

$('""""No worries!"""""""')

The Google JavaScript Style Guideagrees.

谷歌的JavaScript风格指南同意。

回答by Ender

Single quotes are definitely the way to go. I recommend using them for all strings in javascript. Particularly when working with jQuery, it is much more common to need to use a double quote inside a string than a single quote, so it will save you a lot of time escaping and improve readability. Additionally, it'll save you keystrokes, no more pesky shift key :)

单引号绝对是要走的路。我建议将它们用于 javascript 中的所有字符串。特别是在使用 jQuery 时,需要在字符串中使用双引号比单引号更常见,因此它会为您节省大量转义时间并提高可读性。此外,它会为您节省击键次数,不再需要烦人的 shift 键 :)

回答by Tomalak

Single quotes?

单引号?

$(this).prepend('<label for="'+$(this).attr("id")+'">'+'a'+'</label>');

or, jQuery:

或者,jQuery:

$("<label>").attr("for", this.id).text(a).prependTo(this);