简体   繁体   English

jQuery如何将Javascript继续到新行?

[英]jQuery How to Continue Javascript to New Line?

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 ? 如何在jQuery选择器中继续新行? I understand that javascript new lines are simply 我明白javascript新行很简单

(\\) single backslash

Such as

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

Yet using jQuery - this doesn't work when I am listing long selectors ? 然而使用jQuery - 当我列出长选择器时这不起作用? ie

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

Can anyone shed some light how to resolve this ? 任何人都可以解释如何解决这个问题?

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

http://jsfiddle.net/X6QjK/ http://jsfiddle.net/X6QjK/

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')

jQuery is JavaScript. jQuery JavaScript。 Don't be fooled into thinking that there's a difference. 不要误以为这是有区别的。 Is your code working with everything on a single line? 您的代码是否可以在一行中使用所有内容?

I'd guess that your selector is not actually selecting any elements (DOM elements probably don't exist when the selector is called). 我猜你的选择器实际上并没有选择任何元素(调用选择器时可能不存在DOM元素)。

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
});

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. 请注意,仅当您确实需要字符串中的换行符时才需要换行符( \\n )。 Your example would look like this: 您的示例如下所示:

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

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. 这应该工作。

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. 但是如果你不能这样做,那么你可以通过将字符串分成单独的部分并使用+来连接它们来解决问题,而不是通过线条继续字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM