gpt4 book ai didi

Javascript MutationObserver 错误

转载 作者:行者123 更新时间:2023-11-29 19:44:51 28 4
gpt4 key购买 nike

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

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

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

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

Uncaught NotFoundError: An attempt was made to reference a Node in a context where it does not exist. (anonymous function)

最佳答案

chrome执行代码的时候还没有创建Node body。所以你可以这样改变你的代码:

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

或者用 jQuery 做:

    $(document).ready(function(){
//your code
});

关于Javascript MutationObserver 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20439021/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com