admin管理员组文章数量:1356120
I have sets of similar divs that I am toggleing with some basic code like this:
$(".field-group-format-toggler-abstract").click(function()
{
$(".field-group-format-wrapper").toggle();
});
The issue I am having is that whenever I trigger the "+", it toggles all other divs with the same class when I only want to toggle the related div closest to it .field-group-format-wrapper
. I tried .next and .closest but that just seems to lock things up and then it does not work yet I am getting no syntax error e.g.
$(".field-group-format-toggler-abstract").click(function()
{
$(".field-group-format-wrapper").closest().toggle();
});
I created a working version here but if you add in .closest as I have above, it does not work anymore.
/
** Note, I only want to show / hide what's in field-group-format-wrapper
and nothing else so the "Title" still needs to show whether expanded or not.
I have sets of similar divs that I am toggleing with some basic code like this:
$(".field-group-format-toggler-abstract").click(function()
{
$(".field-group-format-wrapper").toggle();
});
The issue I am having is that whenever I trigger the "+", it toggles all other divs with the same class when I only want to toggle the related div closest to it .field-group-format-wrapper
. I tried .next and .closest but that just seems to lock things up and then it does not work yet I am getting no syntax error e.g.
$(".field-group-format-toggler-abstract").click(function()
{
$(".field-group-format-wrapper").closest().toggle();
});
I created a working version here but if you add in .closest as I have above, it does not work anymore.
http://jsfiddle/LHguJ/5/
** Note, I only want to show / hide what's in field-group-format-wrapper
and nothing else so the "Title" still needs to show whether expanded or not.
4 Answers
Reset to default 5What you want is siblings(). And use this to refer to the clicked element. You were using closest which looks for the closest() 'parent'.
$(".field-group-format-toggler-abstract").click(function() {
$(this).siblings(".field-group-format-wrapper").toggle();
});
http://jsfiddle/LHguJ/11/
You could use nextAll()
like this:
jsFiddle
$(".field-group-format-toggler-abstract").click(function(){
$(this).nextAll(".field-group-format-wrapper").toggle();
});
Try using $(this).parent().find(".field-group-format-wrapper")
$(".field-group-format-toggler-abstract").click(function() {
$(this).parent().find(".field-group-format-wrapper").toggle();
});
http://jsfiddle/LHguJ/12/
Or with siblings
:
$(".field-group-format-toggler-abstract").click(function() {
$(this).siblings(".field-group-format-wrapper").toggle();
});
http://jsfiddle/LHguJ/13/
jQuery
$(".field-group-format-toggler-abstract").click(function(){
$(this).siblings(".field-group-format-wrapper").toggle();
$(this).html( $(this).html() == '+' ? '–' : '+');
});
http://jsfiddle/iambriansreed/JLj2X/
本文标签: javascriptJQuery toggle only related div closest to itStack Overflow
版权声明:本文标题:javascript - JQuery toggle only related div closest to it - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744041556a2580754.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论