本文介绍了jQuery - 多个选择器&一次通话中的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种在jQuery中使用多个选择器并为它们设置不同值的方法IN ONE CALL。

I was wondering whether there's a way of using multiple selectors in jQuery and setting different values for them IN ONE CALL.

例如:

$(selector1, selector2, selector3, ...).css({left:[532, 112, 453, ...]});

提前致谢

编辑:为了清楚起见,您只能拨打 .css 一次。是否有可能?

Edited: Just to be clear you can only call .css once. Is it possible?

再次编辑以澄清:我问,因为,例如,如果我调用animate()...我我希望保持一个独特的同步调用/循环。

Edited again to clarify: I'm asking because if, for instance, I call animate()... I'd like to keep a unique synchronized call/loop.

推荐答案

简短回答,不,你不能这样做,选择器你调用的方法没有这样连接。选择器会为您提供一组元素,当发生这种情况时, .css()(或任何其他)函数不知道您所在元素的位置选择器。

Short answer, no you can't do that, the selector and the methods you call aren't connected like that. The selector gets you a set of elements, when that happens the .css() (or any other) function doesn't know what position the element you're in was in the selector.

如果您考虑一下,它可能是所有位置,那么该怎么做?

If you think about it, it could be all positions, then what should it do?

例如:

$("#div, .class1, .class2").css({left:[532, 112, 453]});

这个HTML应该做什么:

What should this do on this HTML:

<div id="div" class="class1 class2"></div>
<div id="div2" class="class2 class1"></div>

你看到它如何让真正的快速混乱,所以不,不支持这样的东西。

You see how it gets confusing real fast, so no, nothing like this is supported.

公平地说, n,如下所示:

To be fair, .css() does take a function, like this:

$("#div, .class1, .class2").css('left', function(i, val) {
  //return new left property here
});

但这些元素按照它们出现在文档中的顺序,而不是与选择器有关。

But these elements are in the order they occur in the document, not related to the selector at all.

这篇关于jQuery - 多个选择器&amp;一次通话中的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:35