天天看點

Javascript Cannot call method 'appendChild' of null 解決方案

Javascript Cannot call method 'appendChild' of null 解決方案:

調試過程中出現一個奇怪的錯誤:Uncaught TypeError: Cannot call method 'appendChild' of null。

隻有在谷歌浏覽器出現這個錯誤,其他IE系列,FF,Opera都是正常。

找了一下原因,js的document.body.appendChild(container);

是在 body 之前運作的,這個時候,document.body 不存在,即為 null ,是以 document.body.appendChild 就會報告上面的錯誤。

解決方案:把SCRIPT 代碼包含到 <body></body> 裡面或者放到body标簽後面即可正常運作.

繼續閱讀