首页 > 解决方案 > IE11:对象不支持属性或方法“replaceWith”

问题描述

我有一个简单的函数,可以在动态加载模式时将它们移动到模式容器中。它适用于 Edge、Chrome 和 Firefox。但是,在 IE11 中,我收到错误消息:“对象不支持属性或方法 'replaceWith'”。我们的客户需要 IE11 支持。

什么可能导致错误?

function moveModals() {
    $('#mainBody .modal').each(function () {
        if ($("#modalsContainer>#" + this.id).exists())
            $("#modalsContainer>#" + this.id)[0].replaceWith(this);
        else
            $(this).appendTo("#modalsContainer");
    });
}

标签: javascriptjquerytwitter-bootstrapinternet-explorer

解决方案


改用这个:

$("#modalsContainer>#" + this.id).replaceWith(this);

这种方式你只依赖于 jQuery([0]从你写的内容中省略)。您编写的方式是从 vanilla HTML DOM 对象调用 replaceWith 。


推荐阅读