admin管理员组

文章数量:1323150

I have a jQuery statement like this;

var current = $(this);
current.hide();
current.siblings('.ab').hide();
current.siblings('.cd').hide();

I want to change this into a single statement and I wrote;

$(current,current.siblings('.ab'),current.siblings('.cd')).hide();

But ab is not hiding. How can I bine the 3 hide() statements into one?

I have a jQuery statement like this;

var current = $(this);
current.hide();
current.siblings('.ab').hide();
current.siblings('.cd').hide();

I want to change this into a single statement and I wrote;

$(current,current.siblings('.ab'),current.siblings('.cd')).hide();

But ab is not hiding. How can I bine the 3 hide() statements into one?

Share edited Apr 2, 2018 at 22:02 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked Apr 10, 2014 at 10:29 AlfredAlfred 21.4k63 gold badges174 silver badges257 bronze badges 1
  • besides the great answer by Frédéric you can bine arbitrary jquery sets with the .add() method – Gabriele Petrioli Commented Apr 10, 2014 at 10:37
Add a ment  | 

5 Answers 5

Reset to default 8

You can use a multiple selector and addBack():

$(this).siblings(".ab, .cd").addBack().hide();

addBack() will add the original element back into the set, so you can get both the element and its relevant siblings in the same jQuery object.

You can use a multiple selector (ma separated) for the siblings function and than use addBack to include the first element.

Add the previous set of elements on the stack to the current set, optionally filtered by a selector.

Code:

current.siblings(".ab, .cd").addBack().hide();

Try to use .end(),

current.siblings(".ab, .cd").hide().end().hide();

or use .add() like below,

current.add(current.siblings(".ab, .cd")).hide();

try this:

   var current = $(this);

    current.hide().siblings('.ab').hide().end().siblings('.cd').hide();

You can use ma separated multiple selectors in .siblings()

 current.siblings('.cd,.ab').addBack().hide();

Working Demo

本文标签: javascriptHow can I combine these jQuery statementsStack Overflow