admin管理员组文章数量:1323317
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?
-
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
5 Answers
Reset to default 8You 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
版权声明:本文标题:javascript - How can I combine these jQuery statements? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742142180a2422629.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论