Internet Explorer Javascript 的 AppendChild 问题
下面的代码在 Firefox 和 Chrome 中工作正常,但在 IE 中却让我头疼。
var anotherDiv= document.getElementById("anotherDiv");
var destination = document.getElementById("mySourceDiv");
destination.appendChild(anotherDiv);
我正在尝试获取一个 Div 元素并将其放置在另一个 div 中。 我收到一条类似于“接口不支持”的错误消息(在 IE 的调试控制台中),并将我指向appendChild 行。 我所看到的是目标变量的类型是一个对象而不是 DOM 元素。
我该怎么做才能将 anotherDiv 附加到 mySourceDiv? 我正在 IE 8 中尝试这个。
The following piece of code, works correctly in Firefox and Chrome, but it gives me a headache in IE.
var anotherDiv= document.getElementById("anotherDiv");
var destination = document.getElementById("mySourceDiv");
destination.appendChild(anotherDiv);
I'm trying to get a Div element and place it inside another div.
I get an error message (in the debug console in IE) similar to "interface not supported", and points me to the appendChild line.
What I've seen is that the type of the destination variable is an object rather then a DOM element.
What can I do to append the anotherDiv to mySourceDiv?
I'm trying this in IE 8.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要诸如 importNode 之类的东西,周围有各种跨浏览器解决方案。问题是每个节点上都有一个相应的文档对象,在 IE 中,所谓的安全性并不能很好地将内容从一个文档移动到另一个文档。
所以,本质上它是在做深度克隆,但使用cloneNode的区别在于cloneNode还设置了你不想要的文档。
这可能会让你朝着正确的方向前进:
IE 对 DOM importNode 的支持
You probably will need something like an importNode, there are various cross browser solutions around. The issue is that each node has a corresponding document object on it, in IE and so called security doesn't play nice moving things from one document to another.
So, essentially it's doing a deep clone, but the difference between using cloneNode is that cloneNode also sets the document which you don't want.
This might get you going in the right direction:
IE support for DOM importNode
我建议使用一个旨在为您解决浏览器不兼容性问题的库。我个人发现 jQuery 非常好。 jQuery 有一个 append 函数。
I'd recommend using a library designed to sort through the browser incompatibilities for you. I've personally found jQuery to be quite good. jQuery has an append function.