我需要通过"侄子"选择器访问类以添加过滤器。如果仅添加父项,则仅获取第一个父项。你能帮我找第二个父母吗?我尝试了.parent((.parent((,但没有成功
<div class="all-types">
<div class="row typeprices mar0 js_item"></div>
</div>
<div class="box-show-alltypes"><a class="js_show_alltypes">GO</a></div>
$('.js_show_alltypes').click(function (e) {
e.preventDefault();
$(this).parent().find('.js_item').slideDown();
$(this).hide();
});
您可以使用
.closest()
/.parent()
获取当前元素父级,然后在病房后使用 .prev()
定位其同级元素.find()
以定位元素
$(this).closest('.box-show-alltypes').prev().find('.js_item').slideDown();
如果您可以将元素包装在公共父项中,即 然后.common-parent
只需使用.closest()
遍历到它并直接使用.find()
$(this).closest('.common-parent').find('.js_item').slideDown();
$('.js_show_alltypes').click(function (e) {
e.preventDefault();
$(this).closest('.box-show-alltypes').prev().find('.js_item').slideDown();
$(this).hide();
});
.js_item{display:none}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="all-types">
<div class="row typeprices mar0 js_item">js_item</div>
</div>
<div class="box-show-alltypes"><a class="js_show_alltypes">GO</a></div>