使用jQuery执行以下操作的最佳(通用)方法是什么?点击.story-collapse
中的主播时,我想在其所在的.collapse
中切换div article
。此article
在页面上重复多次,当然只有当前的人应该切换。
<article>
<div class="story-body clearfix">
<div class="story-paragraph">
<p>This is shown</p>
<div class="collapse">
<p>This is hidden</p>
</div>
</div>
</div>
<div class="story-collapse"><a class="icon icon-icon-arrow-up" href="#"></a></div>
</article>
答案 0 :(得分:1)
您可以使用:
$('.story-collapse').click(function(){
$(this).closest('article').find('.collapse').toggle();
// or
$(this).parent().prev().find('.collapse').toggle();
});
答案 1 :(得分:1)
使用closest()向上导航至article
和find()以获取.collapse
$(this).closest('article').find('.collapse').toggle();
$('.story-collapse a').click(function() {
$(this).closest('article').find('.collapse').toggle();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<article>
<div class="story-body clearfix">
<div class="story-paragraph">
<p>This is shown</p>
<div class="collapse">
<p>This is hidden</p>
</div>
</div>
</div>
<div class="story-collapse">
<a class="icon icon-icon-arrow-up" href="#">toggle</a>
</div>
</article>
<article>
<div class="story-body clearfix">
<div class="story-paragraph">
<p>This is shown</p>
<div class="collapse">
<p>This is hidden</p>
</div>
</div>
</div>
<div class="story-collapse">
<a class="icon icon-icon-arrow-up" href="#">toggle</a>
</div>
</article>
<article>
<div class="story-body clearfix">
<div class="story-paragraph">
<p>This is shown</p>
<div class="collapse">
<p>This is hidden</p>
</div>
</div>
</div>
<div class="story-collapse">
<a class="icon icon-icon-arrow-up" href="#">toggle</a>
</div>
</article>
使用has()
或
$('article').has(this).find('.collapse').toggle();
$('.story-collapse a').click(function() {
$('article').has(this).find('.collapse').toggle();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<article>
<div class="story-body clearfix">
<div class="story-paragraph">
<p>This is shown</p>
<div class="collapse">
<p>This is hidden</p>
</div>
</div>
</div>
<div class="story-collapse">
<a class="icon icon-icon-arrow-up" href="#">toggle</a>
</div>
</article>
<article>
<div class="story-body clearfix">
<div class="story-paragraph">
<p>This is shown</p>
<div class="collapse">
<p>This is hidden</p>
</div>
</div>
</div>
<div class="story-collapse">
<a class="icon icon-icon-arrow-up" href="#">toggle</a>
</div>
</article>
<article>
<div class="story-body clearfix">
<div class="story-paragraph">
<p>This is shown</p>
<div class="collapse">
<p>This is hidden</p>
</div>
</div>
</div>
<div class="story-collapse">
<a class="icon icon-icon-arrow-up" href="#">toggle</a>
</div>
</article>
答案 2 :(得分:1)
您可以使用closest:
$('.story-collapse').on('click',function(){
$(this).closest('article').find('.collapse').toggle();
});
$(this).prev().find('.collapse').toggle();
答案 3 :(得分:0)
父母将选择以前的所有DOM元素,在获取文章元素后,我们可以使用.collapse
方法找到.find()
类元素
$(".story-collapse a").click(function(){
$(this).parents("article").find(".collapse").toggle();
});