简体   繁体   English

如何组合这些jQuery语句?

[英]How can I combine these jQuery statements?

I have a jQuery statement like this; 我有一个像这样的jQuery语句;

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. 但是ab并没有隐藏。 How can I combine the 3 hide() statements into one? 如何将3个hide()语句组合成一个?

You can use a multiple selector and addBack() : 您可以使用多重选择器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. addBack()会将原始元素添加回集合中,因此您可以在同一个jQuery对象中获取元素及其相关的兄弟元素。

Try to use .end() , 尝试使用.end()

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

or use .add() like below, 或者像下面一样使用.add()

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

You can use a multiple selector (comma separated) for the siblings function and than use addBack to include the first element. 您可以为siblings函数使用多重选择器(逗号分隔),而不是使用addBack包含第一个元素。

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 this: 试试这个:

   var current = $(this);

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

You can use comma separated multiple selectors in .siblings() 您可以在.siblings()使用逗号分隔的多个选择器

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

Working Demo 工作演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM