JQuery - Multiple selectors and multiple values ​​in an ONE call

I was wondering if there is a way to use multiple selectors in jQuery and set different values ​​for them in ONE CALL.

eg:.

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

Thanks in advance

Edited: . To be clear, you can only call .css once . Is it possible?

Edited again to clarify: I ask because, for example, if I call animate () ... I would like to keep a unique synchronized call / loop.

+2
source share
4 answers

, , , , . , , .css() ( ) , , , .

, , ?

:

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

HTML:

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

, , , .

, .css() functio n, :

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

, , .

+3

, - .each(), :

$(selector1, selector2, selector3).each(function(idx, element){

  switch(idx)
{
   case : 0
      $(element).css('left', '345');
      break;
   case: 1
      $(element).css('left', '456');
      break;
}
});

EDIT:

, , :

$(document).ready(
    function() {


    $("div#hello, div#hello1").css('background-color', function(idx, element){

        switch(idx)
        {
            case 0:
                return 'red'

            case 1:
                return 'green'
        }
    });

});

HTML:

<div id="hello">
    <span>test</span>
</div>

<div id="hello1">
    <span>test2</span>
</div>
+1

css:

var values = [532, 112, 453, ...];
$(selector1, selector2, selector3, ...).css('left', function (i, current) {
    return values[i];
});

?

jQuery . ( , ).

0

- ( , , .css() )

Please note: 1. selector1, selector2, selector 3 can match el1, el4, el5, el6 2. Just because something is done in one function call does not mean that it is efficient 3. Doing what you ask for doesn't make the code more readable - it makes sense for the left, but what about some other properties

0
source

Source: https://habr.com/ru/post/1747877/


All Articles