2

以下脚本有效,但chrome说有错误..
我应该怎么做才能解决这个错误?

function Do(){alert("test");}

new MutationObserver(Do).observe(document.body,{childList:true,subtree:true}); //works, but there's error.

chrome控制台中的错误消息是这样的;

Uncaught NotFoundError:试图在不存在的上下文中引用节点。(匿名函数)

4

1 回答 1

3

chrome 执行代码时,尚未创建 Node 主体。因此,您可以像这样更改代码:

    window.onload=function(){
      function Do(){alert("test");};
      new MutationObserver(Do).observe(document.body,{childList:true,subtree:true});
    };

或使用 jQuery:

    $(document).ready(function(){
    //your code
    });
于 2013-12-07T09:53:15.697 回答