我试图在每个“动画”类中找到一个“提示”类,然后如果在其中找到提示类,则使用 show()
显示它们的“动画”类。
我的代码似乎有问题,因为它根本无法工作。
我的尝试:
if($('.animate').find('.tip').length === 1) {
$(this).show();
}
HTML
<div class="foobar">
<div class="animate">
<div class="tip">- This is a tip.</div>
</div>
<div class="animate">
<div>- This is not a tip.</div>
</div>
<div class="animate">
<div class="tip">- This is a tip.</div>
</div>
</div>
最佳答案
你需要依次考虑每个$('.animate')
;
$('.animate').each(function () {
if ($(this).find('.tip').length) {
$(this).show();
}
});
...虽然您也可以使用 has()
selector一行完成;
$('.animate:has(.tip)').show();
但是,我猜您还想隐藏与选择器不匹配的元素,因此您可以通过 ( demo );
$('.animate').each(function () {
$(this).toggle(!!$(this).find('.tip').length);
});
... 或 ( demo ):
$('.animate').hide().filter(':has(.tip)').show();
关于javascript - Show() 当类 "tip"被发现不工作时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23188558/