jQuery 如何让 Javascript 继续换行?

发布于 2024-12-05 23:21:13 字数 437 浏览 2 评论 0原文

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

如何在 jQuery 选择器中继续换行?我知道 javascript 新行只是

(\) 单反斜杠

例如

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

Yet using jQuery - 当我列出长选择器时这不起作用?即

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

任何人都可以阐明如何解决这个问题吗?

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

(\) 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.

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

Can anyone shed some light how to resolve this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

牵强ㄟ 2024-12-12 23:21:13
$('#id, .class, .class1, .class2, .class3, .class4, .class5, ' + 
  '.class6, .class7').click(function() {
      //some stuff
});

http://jsfiddle.net/X6QjK/

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

http://jsfiddle.net/X6QjK/

没︽人懂的悲伤 2024-12-12 23:21:13

$() 之间只是一个字符串。话虽如此,你如何将一根绳子分成两部分?

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

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

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

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')
缱倦旧时光 2024-12-12 23:21:13

今天,另一个选择是使用模板文字 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
});

Today another option is using 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
});
吾性傲以野 2024-12-12 23:21:13

jQuery JavaScript。不要被愚弄,认为存在差异。您的代码是否可以在一行上处理所有内容?

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

jQuery is 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).

壹場煙雨 2024-12-12 23:21:13

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

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

请注意,仅当您确实希望在字符串中包含换行符时才需要换行符 (\n)。你的例子看起来像这样:

$('#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:

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:

$('#id, .class, .class1, .class2, .class3, .class4, .class5, ' +
  '.class6, .class7').click(function() {
    //some stuff
});
自控 2024-12-12 23:21:13

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

$('#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.

格子衫的從容 2024-12-12 23:21:13

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

我的建议是通过将您想要应用的每个元素提供给单个共享类来简化事情,这将不需要像这样的过长的选择器字符串。

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

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文