admin管理员组文章数量:1026989
Say I have a <dl>
with all the <dd>
s hidden. Clicking on a <dt>
toggles the <dd>
s that follow it using the following code:
$(this).nextUntil('dt').toggle();
/
Now, I want to automatically hide the <dd>
s following the other <dt>
s, so I try to grab the siblings with this code:
$(this).nextUntil('dt').toggle()
.siblings().filter('dd').hide();
/
But nothing happens, because each <dd>
I've already selected with .nextUntil
is a sibling to each other. As a result, they're all hidden and nothing gets shown.
There must be a pact way to tell jQuery to select all the siblings EXCEPT those I've already selected, but I can't see it. Ideas?
Say I have a <dl>
with all the <dd>
s hidden. Clicking on a <dt>
toggles the <dd>
s that follow it using the following code:
$(this).nextUntil('dt').toggle();
http://jsfiddle/mblase75/FZQj7/
Now, I want to automatically hide the <dd>
s following the other <dt>
s, so I try to grab the siblings with this code:
$(this).nextUntil('dt').toggle()
.siblings().filter('dd').hide();
http://jsfiddle/mblase75/FZQj7/1/
But nothing happens, because each <dd>
I've already selected with .nextUntil
is a sibling to each other. As a result, they're all hidden and nothing gets shown.
There must be a pact way to tell jQuery to select all the siblings EXCEPT those I've already selected, but I can't see it. Ideas?
Share Improve this question edited Jul 20, 2012 at 16:07 Blazemonger asked Jul 20, 2012 at 15:09 BlazemongerBlazemonger 93.1k27 gold badges145 silver badges181 bronze badges4 Answers
Reset to default 3How about this? Notice the use of the not
function, which you can read about here.
http://jsfiddle/lbstr/FZQj7/6/
$('dt').on('click',function() {
var $this = $(this),
$firstGroup = $this.nextUntil('dt');
$firstGroup.toggle();
$this.siblings('dd').not($firstGroup).hide();
});
You could do it from the parent:
$('dt').on('click',function() {
$(this).nextUntil('dt').toggle().siblings("dt").not(this).nextUntil('dt').hide();
});
http://jsfiddle/FZQj7/7/
A simple solution is to apply a class to the elements you show. On each click, you can hide the elements with this class before showing the desired set.
http://jsfiddle/FZQj7/11/
$('dt').on('click',function() {
$('.visibledd').hide().removeClass('visibledd');
$(this)
.nextUntil('dt')
.toggle()
.addClass('visibledd');
});
Here's something a little simpler than these others:
$('dt').on('click',function() {
$(this).siblings('dd').hide();
$(this).nextUntil('dt').show();
});
Say I have a <dl>
with all the <dd>
s hidden. Clicking on a <dt>
toggles the <dd>
s that follow it using the following code:
$(this).nextUntil('dt').toggle();
/
Now, I want to automatically hide the <dd>
s following the other <dt>
s, so I try to grab the siblings with this code:
$(this).nextUntil('dt').toggle()
.siblings().filter('dd').hide();
/
But nothing happens, because each <dd>
I've already selected with .nextUntil
is a sibling to each other. As a result, they're all hidden and nothing gets shown.
There must be a pact way to tell jQuery to select all the siblings EXCEPT those I've already selected, but I can't see it. Ideas?
Say I have a <dl>
with all the <dd>
s hidden. Clicking on a <dt>
toggles the <dd>
s that follow it using the following code:
$(this).nextUntil('dt').toggle();
http://jsfiddle/mblase75/FZQj7/
Now, I want to automatically hide the <dd>
s following the other <dt>
s, so I try to grab the siblings with this code:
$(this).nextUntil('dt').toggle()
.siblings().filter('dd').hide();
http://jsfiddle/mblase75/FZQj7/1/
But nothing happens, because each <dd>
I've already selected with .nextUntil
is a sibling to each other. As a result, they're all hidden and nothing gets shown.
There must be a pact way to tell jQuery to select all the siblings EXCEPT those I've already selected, but I can't see it. Ideas?
Share Improve this question edited Jul 20, 2012 at 16:07 Blazemonger asked Jul 20, 2012 at 15:09 BlazemongerBlazemonger 93.1k27 gold badges145 silver badges181 bronze badges4 Answers
Reset to default 3How about this? Notice the use of the not
function, which you can read about here.
http://jsfiddle/lbstr/FZQj7/6/
$('dt').on('click',function() {
var $this = $(this),
$firstGroup = $this.nextUntil('dt');
$firstGroup.toggle();
$this.siblings('dd').not($firstGroup).hide();
});
You could do it from the parent:
$('dt').on('click',function() {
$(this).nextUntil('dt').toggle().siblings("dt").not(this).nextUntil('dt').hide();
});
http://jsfiddle/FZQj7/7/
A simple solution is to apply a class to the elements you show. On each click, you can hide the elements with this class before showing the desired set.
http://jsfiddle/FZQj7/11/
$('dt').on('click',function() {
$('.visibledd').hide().removeClass('visibledd');
$(this)
.nextUntil('dt')
.toggle()
.addClass('visibledd');
});
Here's something a little simpler than these others:
$('dt').on('click',function() {
$(this).siblings('dd').hide();
$(this).nextUntil('dt').show();
});
本文标签: javascriptHow do I select siblings() of multiple elementsStack Overflow
版权声明:本文标题:javascript - How do I select siblings() of multiple elements? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745671023a2162455.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论