jQuery-在相同级别选择div [英] jQuery - select div at same level
本文介绍了jQuery-在相同级别选择div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在单击按钮时选择特定的div ...唯一的问题是,它必须是buttonClicked的父div的div ...示例:
I want to select a specific div, when clicking on a button... The only issue is, it has to be the div of the buttonClicked's parent div... Sample:
<div class="container">
<div class="box">
<h2>Langtidsparkering</h2>
<div class="content">
Lorem ipsum dolor sit amet..
</div>
</div>
<div class="listcontainer">
<div class="list"> (THIS DIV SHOULD GET A CLASS "VISIBLE", WHEN THE BUTTON IS CLICKED)
</div>
<div class="listbar">
<button class="viewPrices" type="submit" title="Open">Se priser<span></span </button>
</div>
</div>
</div>
代码:
$(".viewPrices").click(function () {
$(".viewPrices").parents('.listaccordion .list').toggleClass('visible');
});
有什么建议吗? :-)
Any suggestions ? :-)
推荐答案
这应该做到.
.closest
将通过父级向上移动,直到找到匹配项.然后,您可以.find
您要查找的目标div.
.closest
will go up through the parents until it finds a match. Then from that you can .find
the target div that you are looking for.
$(".viewPrices").click(function () {
$(this).closest('.listcontainer').find('.list').toggleClass('visible');
});
这是更新的小提琴: http://jsfiddle.net/n264v/2/
这篇关于jQuery-在相同级别选择div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文