3

jQuery

$(".drop-down h3").click(function(e) {
  e.preventDefault();
  $(this).parent(".drop-down").find($("ul")).stop().slideToggle();
  $(this).parent(".drop-down").find($(".divider-aside")).stop().toggle("slow");
  $(this).parent(".drop-down").find($(".arrow")).stop().toggleClass("rotate1 rotate2");
});

HTML

<div id="categories">
  <div class="drop-down">
    <h3>Categories</h3>
  </div>
  <div class="divider-aside"></div>
  <ul>
    <li>123</li>
    <li>12323</li>
    <li>1231</li>
    <li>523</li>
    <li>31</li>
  </ul>
</div>

我想隐藏.drop-down课堂上的所有内容,不包括<h3>点击<h3>。在这种情况下,只有.arrowtoggleClass 有效。

4

2 回答 2

3

使用closest 代替parent

$(this).closest("#categories")

parent 只会返回 1 级,即直接父级。但是你必须得到包含所有 3 个元素的容器

所以 $(this).parent(".drop-down")

应该是

$(this).parent().parent()   // this will break if there is an extra
                            // parent container gets added

或者

$(this).closest("#categories") // This will work even if the no of
                               // parent container keep chaning
于 2013-08-06T18:22:51.723 回答
0

如果您需要全部切换它们,但 .drop-down .siblings 正是您所需要的

$("div.drop-down > h3").click(function(){
    var $t = $(this);
    $t.parent().siblings().toggle();
});
于 2013-08-06T18:29:49.627 回答