javascript - Show() 当类 "tip"被发现不工作时

标签 javascript jquery html

我试图在每个“动画”类中找到一个“提示”类,然后如果在其中找到提示类,则使用 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/

相关文章:

c# - 在 asp.net C# 页面中提醒

javascript - 在动态创建的 `<select>` 上加载选定的选项?

javascript - nodejs-错误 : spawn ENOENT while adjusting image size using module gm

javascript - 如何使用 JQuery 替换 <ul> 的所有 <li> 元素?

javascript - 如果用户向下滚动(水平滚动),则向左滚动

javascript - 将元素与 DOMNodeInserted 一起使用

javascript - 键盘不活动 x 英里秒后自动提交表单

html - 如何在不关闭 html/css 模式的情况下更改图像

javascript - 我的计时器不会更新

javascript - 获取jQuery中隐藏字段的值