Internet Explorer和removeChild() [英] Internet explorer and removeChild()

查看:62
本文介绍了Internet Explorer和removeChild()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用elem.removeChild()从文档中删除元素,但是保存了对该元素的JavaScript引用,以便可以在适当的时候将其添加回去.在Firefox和Chrome中可以正常工作.

I have been using elem.removeChild() to remove elements from my document, but saving a JavaScript reference to that element, so that I can add them back when appropriate. Works just fine in Firefox and Chrome.

现在我注意到在IE7上,这些元素在删除所有子元素的过程中被破坏了.当我将它们添加回相同的父元素时,它们是相同类型的元素,并且保留了诸如类名之类的内容,但是它们没有子元素.

Now I notice that that on IE7, those elements get destroyed in the process, having all their children removed. When I add them back to the same parent element, they are the same type of element and have retained things like their class name, but they have no children elements.

这是预期的行为吗?我知道我可以更改我的应用程序以执行不同的操作,但是这需要花费大量的时间进行重做,我显然希望避免这种情况.我一直认为通过使用removeChild()或通过将父级的innerHTML设置为空字符串来删除元素是可以的,只要我对元素有引用(即,变量指向元素,而不仅仅是元素ID),可以自由添加和删除元素而不会弄乱元素.

Is this expected behavior? I know I can change my app to do things differently, but it would take a good couple hours of reworking and I'd obviously like to avoid that. I've always assumed that it is ok to remove elements, either by using removeChild() or by setting the parent's innerHTML to an empty string, and as long as I had a reference to the element (i.e. a variable points to the element, not just an element id), it was ok to add and remove elements freely without having the element messed up.

这是IE的错误吗?我是否感到困惑并且正在发生其他事情?或者这是已知的和预期的行为?

Is this a bug with IE, am I somehow confused and something else is going on, or is this known and expected behavior?

推荐答案

removeChild的规范并未明确指出要删除的节点的子代应与该节点一起保存,尽管在我看来,将它们保留下来是合乎逻辑的FF和Chrome开发人员也应该这样做,这显然也是.我不知道如果将父对象的innerHTML设置为空字符串时规范会怎么做,但我认为这种方式有点像说擦掉那里的所有内容",因此在这种情况下,我认为这样做是合理的浏览器会丢弃该innerHTML中的所有内容,即使代码中对某些已删除元素的引用也是如此.

The spec for removeChild doesn't explicitly say that the children of the node being removed should be kept along with that node, although to me it seems logical that they should and obviously that is what the FF and Chrome developers decided too. I don't know what the spec says to do if the parent's innerHTML is set to an empty string, but in my opinion that way is a bit like saying "wipe over whatever was there" so in that case I think it is reasonable for the browser to throw away everything that was in that innerHTML even if there were references in code to some of the removed elements.

这篇关于Internet Explorer和removeChild()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆