<!DOCTYPE html><html><head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>DOM方法</title> <link rel="stylesheet" href=""> <script> var ele = document.createElement('div'); var txt = document.createTextNode('textContent'); ele.appendChild(txt); document.getElementsByTagName('body')[0].appendChild(ele); //console中運(yùn)行沒有問題,執(zhí)行這段語句的時(shí)候就報(bào)錯(cuò) </script></head><body></body></html>報(bào)錯(cuò)是出在 document.getElementsByTagName('body')[0]appendChild(ele)這句話,顯示錯(cuò)誤:Uncaught TypeError: Cannot read property 'appendChild' of undefined新手,找了半天不知到錯(cuò)在哪了,求解答。謝謝各位 !
為什么在這段代碼在console下執(zhí)行不報(bào)錯(cuò),但在網(wǎng)頁中就執(zhí)行失敗?
翻翻過去那場雪
2018-08-10 10:09:59