jQuery 如何继续 Javascript 到新行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7487025/
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-26 00:16:34  来源:igfitidea点击:

jQuery How to Continue Javascript to New Line?

javascriptjqueryformatting

提问by Tom

This is lingering question I have and while it may seem like a noob - that's ok as I am one.

这是我挥之不去的问题,虽然它可能看起来像一个菜鸟 - 没关系,因为我就是这样。

How do I continue a new line in jQuery selectors ? I understand that javascript new lines are simply

如何在 jQuery 选择器中继续换行?我知道 javascript 新行很简单

(\) single backslash

(\) single backslash

Such as

var someString = 'abc\
defghi';
alert(someString);

Yet using jQuery - this doesn't work when I am listing long selectors ? i.e.

然而使用 jQuery - 当我列出长选择器时这不起作用?IE

$('#id, .class, .class1, .class2, .class3, .class4, .class5, \
   .class6, .class7').click(function() {
      //some stuff
});

Can anyone shed some light how to resolve this ?

谁能解释一下如何解决这个问题?

回答by AlienWebguy

$('#id, .class, .class1, .class2, .class3, .class4, .class5, ' + 
  '.class6, .class7').click(function() {
      //some stuff
});

http://jsfiddle.net/X6QjK/

http://jsfiddle.net/X6QjK/

回答by Neil

What's inbetween the $()is simply a string. With that said, how would you split a string into two pieces?

中间的$()只是一个字符串。话虽如此,您将如何将字符串分成两部分?

$('big selection text' +
  'more selection' + 
  'more selection still')

Alternatively, you could add to your collection after your initial selection:

或者,您可以在初始选择后添加到您的收藏中:

$('big selection text')
    .add('more selection')
    .add('more selection still')

回答by zzzzBov

jQueryisJavaScript. Don't be fooled into thinking that there's a difference. Is your code working with everything on a single line?

jQueryJavaScript。不要误以为有区别。您的代码是否在一行中处理所有内容?

I'd guess that your selector is not actually selecting any elements (DOM elements probably don't exist when the selector is called).

我猜你的选择器实际上没有选择任何元素(调用选择器时 DOM 元素可能不存在)。

回答by Octavioamu

Today another option is using template literals https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

今天另一个选择是使用模板文字https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

$(`#id, 
   .class, 
   .class1, 
   .class2, 
   .class3, 
   .class4, 
   .class5,
   .class6, 
   .class7`).click(function() {
  //some stuff
});

回答by maerics

In JavaScript, strings cannot span multiple lines, so to encode a multiline string you would do the following:

在 JavaScript 中,字符串不能跨越多行,因此要对多行字符串进行编码,您需要执行以下操作:

var s = "This is a\n" +
        "multiline\n" +
        "string.\n";

Note that the newline characters (\n) are only required if you really want newlines in the string. Your example would look like this:

请注意,\n仅当您确实需要字符串中的换行符时才需要换行符 ( )。您的示例如下所示:

$('#id, .class, .class1, .class2, .class3, .class4, .class5, ' +
  '.class6, .class7').click(function() {
    //some stuff
});

回答by jyore

You can just close the string and use the +operator to add the strings.

您可以关闭字符串并使用+运算符添加字符串。

$('#id, .class, .class1, .class2, .class3, .class4, .class5,' +
   '.class6, .class7').click(function() {
      //some stuff
});

This should work.

这应该有效。

回答by Spudley

I suspect the reason for this not working for you is that when you feed the string over multiple lines like this, it obviously includes a new line character. Possibly jQuery doesn't like this newline being in its selector string.

我怀疑这对您不起作用的原因是当您在多行中输入字符串时,它显然包含一个换行符。可能 jQuery 不喜欢这个换行符出现在它的选择器字符串中。

My advice would be to simplify things by giving each of the elements you want this to apply to a single shared class, which would negate the need to have excessively long selector strings like this.

我的建议是通过将您希望将其应用于单个共享类的每个元素来简化事情,这将消除像这样过长的选择器字符串的需要。

But if you can't do that, then you can resolve the problem by breaking the string into separate sections and using +to concatenate them, rather than continuing the string over a line line.

但是如果你不能这样做,那么你可以通过将字符串分成单独的部分并使用+连接它们来解决问题,而不是在一行上继续字符串。