2017-03-06 42 views
-1

當然有人可以很快回答這個問題!我有一些html標記是類似div的循環,每個div都有一個顯示/隱藏div的按鈕。我正在努力與jQuery找到最近的「目標股利」來切換它的顯示類。查找循環內的特定div並添加類

所以,對於每個「companyItemWrapper」時在其內的「execSummaryLink」按鈕用戶點擊 - 它應該切換類最接近「companyItemWrapperOverlayWrapper」分區的

實施例的代碼是:

<div class="companyItemWrapper"> 
<h3>Company Name</h3> 
<p><a href="#" title="Latest executive summary" class="execSummaryLink">Latest executive summary</a></p> 

<!-- OVERLAY --> 
<div class="companyItemWrapperOverlayWrapper"> 
<p>content...</p> 
</div> 
<!-- end of OVERLAY --> 

</div> 
<!-- end of COMPANY ITEM --> 

<div class="companyItemWrapper"> 
<h3>Company Name</h3> 
<p><a href="#" title="Latest executive summary" class="execSummaryLink">Latest executive summary</a></p> 

<!-- OVERLAY --> 
<div class="companyItemWrapperOverlayWrapper"> 
<p>content...</p> 
</div> 
<!-- end of OVERLAY --> 

</div> 
<!-- end of COMPANY ITEM --> 

<div class="companyItemWrapper"> 
<h3>Company Name</h3> 
<p><a href="#" title="Latest executive summary" class="execSummaryLink">Latest executive summary</a></p> 

<!-- OVERLAY --> 
<div class="companyItemWrapperOverlayWrapper"> 
<p>content...</p> 
</div> 
<!-- end of OVERLAY --> 

</div> 
<!-- end of COMPANY ITEM --> 

回答

3

試試這個解決方案。

$('.execSummaryLink').click(function() { 
 
    $(this).parents(".companyItemWrapper").find(".companyItemWrapperOverlayWrapper").toggle() 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="companyItemWrapper"> 
 
<h3>Company Name</h3> 
 
<p><a href="#" title="Latest executive summary" class="execSummaryLink">Latest executive summary</a></p> 
 

 
<!-- OVERLAY --> 
 
<div class="companyItemWrapperOverlayWrapper"> 
 
<p>content...</p> 
 
</div> 
 
<!-- end of OVERLAY --> 
 

 
</div> 
 
<!-- end of COMPANY ITEM --> 
 

 
<div class="companyItemWrapper"> 
 
<h3>Company Name</h3> 
 
<p><a href="#" title="Latest executive summary" class="execSummaryLink">Latest executive summary</a></p> 
 

 
<!-- OVERLAY --> 
 
<div class="companyItemWrapperOverlayWrapper"> 
 
<p>content...</p> 
 
</div> 
 
<!-- end of OVERLAY --> 
 

 
</div> 
 
<!-- end of COMPANY ITEM --> 
 

 
<div class="companyItemWrapper"> 
 
<h3>Company Name</h3> 
 
<p><a href="#" title="Latest executive summary" class="execSummaryLink">Latest executive summary</a></p> 
 

 
<!-- OVERLAY --> 
 
<div class="companyItemWrapperOverlayWrapper"> 
 
<p>content...</p> 
 
</div> 
 
<!-- end of OVERLAY --> 
 

 
</div> 
 
<!-- end of COMPANY ITEM -->

+0

謝謝 - 將接受這一次我可以做 – dubbs

+0

@dubbs樂意幫助 –

0
$(".execSummaryLink").click(function(event) { 
    event.preventDefault(); 
    $(this).closest('.companyItemWrapper').find('.companyItemWrapperOverlayWrapper').toggleClass('XXXX'); 
}) 
1
$(". execSummaryLink").on("click",function(){ 
    event.preventDefault(); 
    $(this).parent(".companyItemWrapper").find(". companyItemWrapperOverlayWrapper").toggleClass("anotherClass"); 
}); 

應該這樣做。

+0

事件應該作爲函數參數傳遞 –