简体   繁体   English

Javascript隐藏div(is:empty)问题

[英]Javascript hide div (is:empty) issue

Have some code to hide a div, if it's contents are empty. 有一些代码隐藏div,如果它的内容是空的。 This SHOULD work...its' worked before. 这应该工作......它以前工作过。 Am I missing something obvious here? 我错过了一些明显的东西吗? The div class = "msgalert" div class =“msgalert”

$(document).ready(function () {
    if ($('.msgalert').is(':empty')) {
        $(".msgalert").css({'display':'none'});
    }
});

The corresponding CSS is: 相应的CSS是:

.msgalert { border: 1px solid #eac572; background-color: #ffe9ad; }

Please help! 请帮忙! Thanks :) 谢谢 :)

Try checking if the length of the html() of .msgalert is 0 instead of checking if it's :empty , like this: 尝试检查.msgalerthtml()长度.msgalert为0而不是检查它是否为:empty :如下所示:

if ($.trim($('.msgalert').html()).length == 0) {

You'll want to trim the html() to account for the whitespace as well. 您还需要修剪html()以考虑空白。

Just posting my version, 只是发布我的版本,

$('.msgalert').filter(function () {
   return ($(this).text().length == 0 && $(this).children().length == 0);
}).css('display', 'none');

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

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