Javascript 单击按钮时如何切换所有div

Javascript 单击按钮时如何切换所有div,javascript,jquery,html,toggle,show-hide,Javascript,Jquery,Html,Toggle,Show Hide,需要关闭(隐藏)所有.body divs=?(div.body{display:none}?) 当单击.按钮时,需要关闭(隐藏)所有.body打开的div 需要打开最近的.body div,单击 单击.button时需要更改.button图标(负图标或正图标) HTML 风格 这将起作用: $('.button')。在('click',function()上{ $btn=$(此项); $('.body').hide(); $('.box.negative').removeClass('nega

需要关闭(隐藏)所有.body divs=?(div.body{display:none}?)

当单击.按钮时,需要关闭(隐藏)所有.body打开的div

需要打开最近的.body div,单击

单击.button时需要更改.button图标(负图标或正图标)

HTML 风格

这将起作用:

$('.button')。在('click',function()上{
$btn=$(此项);
$('.body').hide();
$('.box.negative').removeClass('negative').addClass('positive');
$btn.closest('.box').find('.body').toggle();
$btn.find('span')。toggleClass('negative');
});
.body{
显示:无;
}
.按钮.阳性:在{
内容:“样本(+)”;
}
.按钮.负片:之前{
内容:'样本(-);
}

一些文本
一些文本
这将起作用:

$('.button')。在('click',function()上{
$btn=$(此项);
$('.body').hide();
$('.box.negative').removeClass('negative').addClass('positive');
$btn.closest('.box').find('.body').toggle();
$btn.find('span')。toggleClass('negative');
});
.body{
显示:无;
}
.按钮.阳性:在{
内容:“样本(+)”;
}
.按钮.负片:之前{
内容:'样本(-);
}

一些文本
一些文本

这不会解决所有问题,但对于初学者来说,
jQuery.closest()
无效
.closest()
是jQuery元素上的函数,而不是jQuery本身。它需要说
jQuery(this)。最近的(…)
不要试图重新发明轮子,向那些已经做过的人学习:@webeno我不能在我的项目或任何框架中使用Bootstrap,thxI不一定意味着你要实现Bootstrap,与其查看相关代码并将其复制…@webeno这比简单的手风琴更麻烦。您可以在5行代码中实现此效果;)这不会解决所有问题,但对于初学者来说,
jQuery.closest()
无效
.closest()
是jQuery元素上的函数,而不是jQuery本身。它需要说
jQuery(this)。最近的(…)
不要试图重新发明轮子,向那些已经做过的人学习:@webeno我不能在我的项目或任何框架中使用Bootstrap,thxI不一定意味着你要实现Bootstrap,与其查看相关代码并将其复制…@webeno这比简单的手风琴更麻烦。您可以在5行代码中实现此效果;)我也有同样的解决办法,但你比我强。我要说的唯一一件事是,您可以将
$('.box按钮span')
简化为
$('.box.negative')
有相同的解决方案,但您比我先解决了。我要说的唯一一件事是,您可以将
$('.box按钮span')
简化为
$('.box.negative')
<div class="box">

  <div class="header">

    <a href="#" class="button">

      <span class="fa positive"></span>

    </a>

  </div>

  <div class="body">

  </div>

</div>
jQuery('.button').on('click', function() {

  jQuery('.body').close();

  jQuery(this).closest(".body, .header").toggle(); // Edit 

  jQuery('span').toggleClass('negative');

  jQuery(this).parents().find('.body').toggle() // i test it, doesn't work

});
.body {
  display:none;
}

.button .positive:before {
  content : "sample ( + )";
}

.button .negative:before {
  content : 'sample ( - )';
}