使用DOMNodeInserted确定.class名称

时间:2013-07-23 10:34:19

标签: javascript jquery dom mutation-events

如果我在下面运行:

$(document).bind('DOMNodeInserted', function(){

      $('.new', this).hide();
});

它会运行正常,它会隐藏.new div。但我需要做类似下面的事情:

$(document).bind('DOMNodeInserted', function(){

          // if class .new exists
          // do something to the other elements e.g (body, #div, h1, h2, etc) not to .new class
});
非常感谢

2 个答案:

答案 0 :(得分:3)

您只需检查.new的长度,然后按以下方式处理:

$(document).bind('DOMNodeInserted', function(){
    if($('.new').length > 0)
    {
        $('body *').not('.new').hide();
    }
});

请参阅此jsFiddle Demo

答案 1 :(得分:2)

试试这个:

$(document).bind('DOMNodeInserted', function () {    
    if ($('.new').length) {
        // if class .new exists
        // do something to the other elements e.g (body, #div, h1, h2, etc) not to .new class
    }
});