我正在尝试在我的 javascript 代码中创建一个新元素并将其附加到我的元素之一。但似乎旧的 IE 浏览器不支持此功能,我的代码在我使用 appendChild 功能的那一行中断。
var child = document.createElement('span');
child.innerHTML = "Hello World!"; // Crashes here
parent.appendChild(child); // And here
有没有我可以用于 IE 的替代品?
谢谢。
P.S 代码在现代浏览器中运行良好。
更新: 下面的代码解决了我问题的最后一部分,我可以将空的 child 附加到 parent 身上:
var child = document.createElement('span');
if (parent.insertAdjacentElement){
parent.insertAdjacentElement('beforeEnd', child);
}
else if (parent.appendChild) {
parent.appendChild(child);
}
但我仍然需要在子元素中放置一些数据。 createTextNode
、innerHTML
、setAttributes
不起作用。
最佳答案
关于javascript - 旧 IE 浏览器中的 appendChild,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1619601/