document.head.appendChild(element) 在 IE8 及以下報錯


問題:

  在開發中會遇到動態添加 script 標簽的情況。

代碼如下:

1 var oScript = document.createElement('script');
2 oScript.src = 'demo_address';
3 document.head.appendChild(oScript);

但是在 IE8 以下會報如下錯誤:

1 SCRIPT5007: Unable to get value of the property 'appendChild': object is null or undefined 

 查看 MDN 之后發現,在 IE9 以下不支持

 

解決辦法:

1 document.getElementsByTagName('head')[0].appendChild(oScript);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM