jQuery 如何让 Javascript 继续换行?
这是我一直挥之不去的问题,虽然它可能看起来像个菜鸟——但这没关系,因为我是菜鸟。
如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
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?Alternatively, you could add to your collection after your initial selection:
今天,另一个选择是使用模板文字 https://developer.mozilla .org/en-US/docs/Web/JavaScript/Reference/Template_literals
Today another option is using template literals https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
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).
在 JavaScript 中,字符串不能跨越多行,因此要对多行字符串进行编码,您可以执行以下操作:
请注意,仅当您确实希望在字符串中包含换行符时才需要换行符 (
\n
)。你的例子看起来像这样:In JavaScript, strings cannot span multiple lines, so to encode a multiline string you would do the following:
Note that the newline characters (
\n
) are only required if you really want newlines in the string. Your example would look like this:您可以关闭字符串并使用
+
运算符添加字符串。这应该有效。
You can just close the string and use the
+
operator to add the strings.This should work.
我怀疑这对你不起作用的原因是,当你像这样在多行中输入字符串时,它显然包含一个新行字符。可能 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.