插入节点时的MutationObserver

时间:2016-04-15 09:25:19

标签: javascript mutation-observers

我希望在dom中插入一个节点时添加一个节点元素。

当我调用observe方法时,出现错误:Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'.

我的代码:

var mutationObserver = new MutationObserver( function(mutations) {
  mutations.forEach( function(mutation) {
    console.log(mutation);
    // here I'd like to insert my new node
  });
});

var options = {
  childList: true
};

var targetNode = document.querySelector('div.nav-bar');
console.log(targetNode); //null - and not null after dom ready
mutationObserver.observe(targetNode, options);

0 个答案:

没有答案