【发布时间】:2009-10-25 00:32:49
【问题描述】:
我正在尝试在我的 javascript 代码中创建一个新元素并将其附加到我的一个元素中。但似乎旧的 IE 浏览器不支持此功能,我的代码在我使用 appendChild 功能的那一行中断。
var child = document.createElement('span');
child.innerHTML = "Hello World!"; // Crashes here
parent.appendChild(child); // And here
我可以为 IE 使用任何替代方案吗?
谢谢。
P.S 代码在现代浏览器中运行良好。
更新: 以下代码解决了我的问题的最后一部分,我可以将空子附加到父级:
var child = document.createElement('span');
if (parent.insertAdjacentElement){
parent.insertAdjacentElement('beforeEnd', child);
}
else if (parent.appendChild) {
parent.appendChild(child);
}
但我仍然需要在子元素中放入一些数据。 createTextNode、innerHTML、setAttributes 不工作。
【问题讨论】:
-
定义“旧 IE”的含义实际上可能会有所帮助。 IE5? 4? 3?
标签: javascript internet-explorer appendchild