我一直在尝试检查一个类是否存在于 div 中。但它总是导致长度为 0。这是我的代码:-
<div id="accordion2" class="accordion" >
<div class="accordion-group">
<div class="accordion-heading"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
<div class="align-nav arrow-active">OSTOMY PRODUCTS</div></a>
</div>
<div id="collapseOne" class="accordion-body in collapse">
<div class="accordion-inner">
<ul>
<li><a href="#">Professional Resources</a></li>
<li><a href="#">One-piece Pouching</a></li>
</ul>
</div>
</div>
</div>
<div class="accordion-group">
<div class="accordion-heading"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo">
<div class="align-nav arrow-inactive">LEARNING CENTER</div>
</a> </div>
<div id="collapseTwo" class="accordion-body collapse">
<div class="accordion-inner">
<ul>
<li><a href="#">Videos</a></li>
<li><a href="#">Before Your Surgery</a></li>
</ul>
</div>
</div>
</div>
<div class="accordion-group">
<div class="accordion-heading"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseThree">
<div class="align-nav">PROFESSIONAL RESOURCES </div>
</a> </div>
</div>
<div class="accordion-group">
<div class="accordion-heading"> <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseThree">
<div class="align-nav">PROFESSIONAL RESOURCES </div>
</a> </div>
</div>
</div>
对于前 2 个 div $(this).find('accordion-body').length 应该是 1。但它仍然显示 0。我的 Jquery 代码如下:-
$('.accordion-heading').click(function() {
if($(this).find('accordion-body').exist()) {
alert('hi');
if($(this).find('.collapsed').length > 0) {
alert('hi');
//$(this).find('.accordion-toggle .align-nav').removeClass('arrow-active').addClass('arrow-inactive');
}
else {
alert('hi');
//$(this).find('.accordion-toggle .align-nav').removeClass('arrow-inactive').addClass('arrow-active');
}
}
});
我需要得到 $(this).find('accordion-body').length = 1; 请提供解决方案。