H5新特性MutationObser报错Failed to execute 'observe' on 'MutationObserver解决

控制台报错:Uncaught TypeError: Failed to execute 'observe' on 'MutationObserver': parameter 1 is not of type 'Node'.

意味着执行需要的是节点(dom元素),不是jq的方法,所以请使用js获取元素;

如下

var targetNode  =  document.querySelector(".f-w6");//而不是用$(".f-w6")等
var config = {
  characterData: true
}
var callback = function(mutationsList) {
  for(var mutation of mutationsList) {
      if (mutation.type == 'characterData') {
          console.log('33333');
      }
  }
};
var observer  = new MutationObserver(callback);
observer.observe(targetNode , config);

 

你可能感兴趣的:(web前端)