如何使用jQuery实时检查元素inset html

How to live check element inset html with jQuery.

本文关键字:元素 inset html 检查 实时 何使用 jQuery      更新时间:2023-09-26

我有一些块,通常解析内容。我需要为它更改一些样式,但如果有一些html的话。我的问题是:有没有可能用jQuery实时检查某个元素在指定时刻是否有html,并在它为空的情况下设置一些操作?谢谢你提供一些信息。

我的代码:

if ($('.degrees').live().html === ("")){
    $('.block').fadeOut('slow');
}

试试这个:

检测空的div并更改其html:

$('.degrees:empty').on('DOMSubtreeModified',function(){
    //...
});

检测每个html更改

$('.degrees').on('DOMSubtreeModified',function(){
    if($(this).is(':empty')) // check if it became empty ..
});

您可以通过以下方式进行检查:

if($('.degrees').html().length > 0){
    // element has children
} else {
    // no children
}

您可以检查元素是否为空,如下所示:

if ($('.degrees').is(':empty')){
    $('.block').fadeOut('slow');
}

但它只有在您进行检查时才有效,而对于未来的元素则无效。要做到这一点,您必须使用一个间隔来定期检查它,这通常不是一个好主意。

您可以检查children元素:

var $main = $('.degrees'),
    $child = $('.block');
if($main.children().length < 0) {
    $child.fadeOut(500);
}