我已经做了几个小时了,感觉快要抓狂了。基本上我需要做的是获取出现在body中的第一个元素,然后在它前面插入另一个元素。
我尝试过以下方法来获取第一个元素,但都没有成功(返回undefined或null)
window.document.body.firstChild
document.getElementsByTagName("body").firstChild
document.getElementsByTagName("body")[0].firstChild
window.document.documentElement.childNodes[1].childNodes[0]
我尝试了许多混合匹配之前的片段,也试过仅获取body然后appendChild(),但都没有成功。
非常感谢您的帮助。
document.body.firstChild
没问题。您确定insertBefore()
的参数顺序正确吗? - Crescent Fresh