How to use multiple jquery object variables as selectors?

后端 未结 3 657
独厮守ぢ
独厮守ぢ 2020-12-03 04:54

In jQuery, selecting more than one element can be done like this:

$(\"#id1,#id2\").show();

But when I have two jQuery objects, I don\'t see

相关标签:
3条回答
  • 2020-12-03 05:21

    You can use add :

    jqId1.add(jqId2).show();
    

    But don't make your code too complex just to avoid querying "#id1,#id2" : this selector relies on getElementById and is very fast.

    0 讨论(0)
  • 2020-12-03 05:24

    I know this sound a little stupid, but you can also try like this.

    $([
        jqId1.get(0),
        jqId2.get(0),
        jqId3.get(0),
        ... // more jQuery elements
    ]).show();
    
    0 讨论(0)
  • 2020-12-03 05:40

    You can use each cycle:

    $([jqId1, jqId2]).each( function(){
        $(this).show();
    });
    

    As answered here: Select multiple jQuery objects with .add()

    0 讨论(0)
提交回复
热议问题