最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery: how to detect when some element is added to dom? - Stack Overflow

programmeradmin1浏览0评论

I want something like:

$('.pac-container').whenAddToDom(function(){
    console.log('element with class pac-container added to DOM');
    //I want to use this added element $(this).doSomethingWithThis();
});

I want something like:

$('.pac-container').whenAddToDom(function(){
    console.log('element with class pac-container added to DOM');
    //I want to use this added element $(this).doSomethingWithThis();
});
Share Improve this question edited Jun 13, 2016 at 13:24 fico7489 asked Jun 13, 2016 at 12:46 fico7489fico7489 8,5707 gold badges59 silver badges93 bronze badges 4
  • 2 Check this question. It may helpful. stackoverflow./questions/3219758/detect-changes-in-the-dom – suyesh Commented Jun 13, 2016 at 12:51
  • 1 Or this question: http://stackoverflow./questions/2844565/is-there-a-javascript-jquery-dom-change-listener – Mikey Commented Jun 13, 2016 at 12:52
  • Mutation Observers is what you are looking for. – Jai Commented Jun 13, 2016 at 12:54
  • 1 you can listen to elements that are being appended .bind('DOMNodeInserted DOMNodeRemoved') – Roysh Commented Jun 13, 2016 at 13:01
Add a ment  | 

2 Answers 2

Reset to default 4

I would wrap the elements that will be appended in a container and do something like this:

$("html").bind("DOMNodeInserted",function(){
    console.log('element with class '+$("#container > *").attr('class') +' added to DOM');
});

Here's a fiddle http://jsfiddle/PgAJT/295/

This is solution to my problem:

$(document).bind('DOMNodeInserted DOMNodeRemoved', function(element){
    if($(this).hasClass('pac-container')){
        console.log(element.target);
    }

});
发布评论

评论列表(0)

  1. 暂无评论