繁体   English   中英

如果div没有内容,则jQuery隐藏父元素

[英]JQuery hiding parent element if div has no content

我有这样的代码。如果div class = content中没有内容,我需要隐藏父div。 谁能告诉我我该怎么做。 看起来很简单,用谷歌搜索大约2-3个小时,但是我无法解决这个问题。

      <div id="row-custom_57" class="section custom_57-section">
        <div class="label">
            Designation
        </div>
         <div class="content">

         </div>
         <div class="clear"></div>
      </div>
      <div id="row-birth_date" class="section birth_date-section">
        <div class="label">
            Date of birth
        </div>
         <div class="content">

         </div>
         <div class="clear"></div>
      </div>
      <div id="row-birth_date" class="section birth_date-section">
        <div class="label">
            Date of birth
        </div>
         <div class="content">

         </div>
         <div class="clear"></div>
      </div>

我尝试过这样的事情。 但没有运气。 它没有进入如果条件..任何人都可以告诉我该怎么做。

  $('.content').each(function(){ console.log($(this).text()); //console.log($(this).html()); if($(this).html() === null) { console.log('innnnn' + $(this).html); //$(this).hide(); //$(this).parent().hide(); //$(this).parent().parent().hide(); } else{ //console.log($(this).html()); } 

非常感谢

内容实际上为空还是为空字符串?

如果您尝试这样做,会发生什么:

if($(this).html().match(/^\s*$/))
{
    console.log('innnnn' + $(this).html);
}

这会做到的:

$('.content').each(function(){
    if($(this).html()==''){
        $(this).parent().hide();
    }
});

$(this).html()返回的值是一个字符串,因此它永远不会为null ,而只会是'' (空)

我认为最好以这种方式进行检查:

$(this).text()

因为这仅返回不包含html标记的文本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM