简体   繁体   English

document.body 评估为 null

[英]document.body evaluates to null

Whenever I try to use document.body.appendChild , it evaluates to null .每当我尝试使用document.body.appendChild时,它都会评估为null Here is my code:这是我的代码:

document.body.appendChild(c92w46);

I get this error:我收到此错误:

main.js:96 Uncaught TypeError: Cannot read property 'appendChild' of null

Most probably it is trying to reach body before DOM is ready.很可能它试图在 DOM 准备好之前到达 body。 You should use DOMContentLoaded event for DOM manipulation.您应该使用DOMContentLoaded事件进行 DOM 操作。

https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event

window.addEventListener('DOMContentLoaded', (event) => {
    document.body.appendChild(<element>);
});

Otherwise, it is better to share more code.否则,最好共享更多代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM