Hide a div if content is empty

前端 未结 3 1256
醉梦人生
醉梦人生 2020-12-15 12:19

Philosophy bubble is like a quote/speech bubble div styled which has a sharepoint control inside, the richHtmlField which lets users to type in content while editing page, b

3条回答
  •  我在风中等你
    2020-12-15 12:56

    Use jQuery's :empty selector:

    $('.philosophy-bubble:empty').hide();
    

    Here's a working fiddle.

    Alternative

    You could also use the filter() function to find all empty div's and hide:

    //All divs
    $('div').filter(function() {
            return $.trim($(this).text()) === ''
    }).hide()
    
    //div's with a certain class
    $('.philosophy-bubble').filter(function() {
            return $.trim($(this).text()) === ''
    }).hide()
    
    //div with a specific ID
    $('#YourDivID').filter(function() {
            return $.trim($(this).text()) === ''
    }).hide()
    

    ..etc.

    Note: the :empty selector will be more performant. See jsPerf.

提交回复
热议问题