javascript IE 不支持“insertBefore”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9377887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:28:33  来源:igfitidea点击:

IE doesn't support 'insertBefore'

javascriptinternet-explorer

提问by Teq1

I have a problem with this piece of code :

我对这段代码有问题:

    var logo = document.getElementById("move_this");
    prependElement('container', logo);

    function prependElement(parentID, child) {
        parent = document.getElementById(parentID);
        parent.insertBefore(child, parent.childNodes[0]);
    }

In IE I have an error:

在 IE 中我有一个错误:

SCRIPT438: Object doesn't support property or method 'insertBefore'

SCRIPT438:对象不支持属性或方法“insertBefore”

Is there a way to resolve this problem?

有没有办法解决这个问题?

回答by Dr.Molle

Use it like that:

像这样使用它:

var parent=document.getElementById(parentID);

otherwise parent will be global, but there always is a global parent-object, the parentwindow(and it is read-only).

否则 parent 将是全局的,但总是有一个全局父对象,即窗口(并且它是只读的)。

Furthermore:IE requires as 2nd argument a valid node or null, so be sure that parent has childNodes to avoid errors:

此外:IE 需要一个有效节点或空作为第二个参数,因此请确保父节点具有 childNodes 以避免错误:

parent.insertBefore(child,(parent.hasChildNodes())
                            ? parent.childNodes[0]
                            : null);

回答by DDM

insertBeforeworks correctly in IEas long as the 2nd parameter is a valid DOM element, or null( typeof nullis Objectand so is a typeofDOM element).

insertBefore在正常工作IE,只要该第二参数是有效DOM元素,或nulltypeof nullObject,所以是一个typeofDOM元素)。

For an Array, any out of bound index (which in this case is 0as the children[]is empty) will return undefined. IE stops working in the following case as the 2nd param becomes undefined-

对于 an Array,任何越界索引(在本例中0children[]空)都将返回undefined。IE 在以下情况下停止工作,因为第二个参数变为undefined-

parent.insertBefore(child, parent.childNodes[0])
//parent.childNodes[INDEX]
//where `INDEX` is greater than parent.childNodes.length

So, a better approach for this case will be

因此,对于这种情况,更好的方法是

var refEl  = parent.childNodes[INDEX] || null;
parent.insertBefore(newRowHolderNode.childNodes[0], refEl);

回答by Dmitry Babich

As it was mentioned above, .insertBeforeworks only with valid node (explicit ID or global document reference). Local variable reference would fail:

如上所述,.insertBefore仅适用于有效节点(显式 ID 或全局文档引用)。局部变量引用会失败:

parent.insertBefore(child, localref); //error

In IE we can use .sourceIndexproperty to get current global index of "marker" element, before which new child is inserted. Here is the fix for IE:

在 IE 中,我们可以使用.sourceIndex属性来获取“标记”元素的当前全局索引,在该索引之前插入新的子元素。这是 IE 的修复:

parent.insertBefore(child, document.all[localref.sourceIndex]);

It is not necessary to use .sourceIndexproperty, if you know exactly where in parent tree you wish to insert an element. Following properties works just fine:

.sourceIndex如果您确切地知道要在父树中插入元素的位置,则没有必要使用属性。以下属性工作正常:

parent.insertBefore(child, parent.all[1]); //must be a child
parent.insertBefore(child, parent.children[2]); //any sane index
parent.insertBefore(child, parent.firstChild); //if at least one child exists
parent.insertBefore(child, parent.lastChild); //if at least one child exists
parent.insertBefore(child, localref.nextSibling]); //if not last child
parent.insertBefore(child, localref.previousSibling]); //if not first child