IE不支持'insertBefore' [英] IE doesn't support 'insertBefore'

查看:208
本文介绍了IE不支持'insertBefore'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这段代码有疑问:

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

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

在IE中我有一个错误:

In IE I have an error:


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

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

是有办法解决这个问题吗?

Is there a way to resolve this problem?

推荐答案

使用它:

var parent=document.getElementById(parentID);

否则父将是全局的,但总是有一个全局父对象, 窗口(和它是只读的。

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

此外:
IE需要第二个参数作为有效节点或null,所以请确保parent有childNodes以避免错误:

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

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

这篇关于IE不支持'insertBefore'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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