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>
.在这种情况下只有 .arrow
toggleClass 有效。
最佳答案
使用closest
代替parent
$(this).closest("#categories")
父级只会返回一级,即直接父级。但是你必须得到包含所有 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
关于javascript - 这个父 jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18087644/