How to hide/remove a DIV when empty

前端 未结 5 1629
梦毁少年i
梦毁少年i 2020-11-29 11:11

I have some server side HTML output I cannot deal with using pure CSS, essentially the DIV sometimes holds:

Content
相关标签:
5条回答
  • 2020-11-29 11:46

    You can do this using only CSS:

    div:empty { display: none }
    
    0 讨论(0)
  • 2020-11-29 11:49

    You can also use CSS-only to solve this, putting a table with style="empty-cells:hide" around the content, and changing the div to a td, like this:

    Before:

    <div>Content</div>

    After:

    <table style="empty-cells:hide"><tr><td>Content</td></tr></table>

    0 讨论(0)
  • Here's another easy way... using the filter()...

    $('div').filter(function() {
    
      return $.trim($(this).text()) === ''
    
    }).remove()
    
    0 讨论(0)
  • 2020-11-29 11:56

    Even better (assuming jQuery):

    $(document).ready(function() { $('div:empty').remove(); });
    

    EDIT: The other answers are good, but the OP wanted to remove the empty item, not hide it.

    0 讨论(0)
  • 2020-11-29 12:00

    I think you can do the following:

    $(function() {
      $("div:empty").hide();
    });
    

    jQuery's empty pseudo selector is great.

    0 讨论(0)
提交回复
热议问题