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クラスを除いてすべてを非表示にしたいと思います。この場合、toggleClass のみが機能します。<h3><h3>.arrow

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 に答える