How do I hide a parent div if the child div doesn't contain an tag?

后端 未结 4 1740
梦毁少年i
梦毁少年i 2021-01-27 07:41

I\'m trying to hide a parent div (i.e., #post) if the child div (i.e., .post-title) doesn\'t contain an tag. Any insight in how to accomplish this correct

4条回答
  •  庸人自扰
    2021-01-27 08:36

    Just use jQuery's toggle using a boolean from checking the length of the children:

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

提交回复
热议问题