Jquery 如果子div没有';t包含一个<;a>;标签

Jquery 如果子div没有';t包含一个<;a>;标签,jquery,Jquery,如果子div(即post title)不包含标记,我试图隐藏父div(即#post)。对于如何用我修改过的代码正确完成这一点,有什么见解吗 <div id="latest"> <div id="post"> <div class="post-image post-image-1"></div> <div id="post-content"> <div class="p

如果子div(即post title)不包含
标记,我试图隐藏父div(即#post)。对于如何用我修改过的代码正确完成这一点,有什么见解吗

<div id="latest">
    <div id="post">
        <div class="post-image post-image-1"></div>
        <div id="post-content">
            <div class="post-title post-title-1"></div>
            <div id="post-date"></div>
        </div>
    </div>
<div id="post">
        <div class="post-image post-image-2"></div>
        <div id="post-content">
            <div class="post-title post-title-2"></div>
            <div id="post-date"></div>
        </div>
    </div>
</div>

第一:不要多次使用IDs。每个ID都应该是唯一的:)

其次:
$('.post').not(':has(“.post title a”)).addClass('hide')


注意:复制
id
s是犯罪行为 您只需使用jQuery的

像这样:

$('.post-title').filter(function(i) { return !$(this).find('a').length }).hide();
见下面的例子
$('.post title').filter(函数(i){return!$(this.find('a').length}).hide()

无链接
无链接
只需使用jQuery,通过检查子项的长度使用布尔值即可:

/* Loop Through Parents */
$('#post').each(function() {
    $(this).toggle($(this).children('a').length > 0);
});

不能复制
id
值。请使用
$.fn.filter
使用
具有
功能。你愿意接受一个适合你的答案吗?@PraveenKumar:D:D这正好相反。这是怎么得到投票的?@PraveenKumar更新:D想成为时髦的人,而不是使用过滤器。。。有点不对劲,但其他答案却起到了作用?甚至这都是错误的
:P
它不应该隐藏在这里,而应该隐藏otherwise.LoL。不,期待你更多。至少要做出正确的回答<代码>:D
@PraveenKumar我很抱歉,因为你的答案一开始是正确的:D没关系,继续努力:)
/* Loop Through Parents */
$('#post').each(function() {
    $(this).toggle($(this).children('a').length > 0);
});