javascript Internet Explorer 在尝试从另一个窗口附加 HTML 对象时抛出 SCRIPT5022:HierarchyRequestError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23097024/
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-28 00:19:51  来源:igfitidea点击:

Internet Explorer throws SCRIPT5022: HierarchyRequestError when trying to appendChild an HTML object from another window

javascriptjqueryhtmlinternet-explorer

提问by phamductri

I have an html table from one window. I get the table by using

我有一个窗口中的 html 表。我通过使用获得表格

var table = document.getElementById('tableId');

then I open another window by using :

然后我使用以下命令打开另一个窗口:

window.open('newWindow.html'); 

In this window, there's a div with id = divId.

在这个窗口中,有一个 id = divId 的 div。

Now if I tried to append the table from the other window by the following code :

现在,如果我尝试通过以下代码从另一个窗口附加表格:

var cloneTable = jQuery.extend(true, {}, window.opener.table);

var div = document.getElementById('divId');

div.appendChild(cloneTable);

Internet Explorer will throw this error :

Internet Explorer 将抛出此错误:

SCRIPT5022: HierarchyRequestError

SCRIPT5022:层次结构请求错误

I have searched into the error code, listed here :

我搜索了错误代码,在此处列出:

http://msdn.microsoft.com/en-us/library/ie/gg592979(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/ie/gg592979(v=vs.85).aspx

It said I can't insert the node at that location. However, the above code works fine in Firefox and Chrome.

它说我无法在该位置插入节点。但是,上面的代码在 Firefox 和 Chrome 中运行良好。

What's happened here and how I can work around it ?

这里发生了什么以及我如何解决它?

Thanks,

谢谢,

P.S: I'm using IE 10

PS:我使用的是 IE 10

回答by NoGray

IE is probably can't move elements across different documents. What you can do is to user the outerHTML property if the browser fails to clone the table.

IE 可能无法在不同的文档中移动元素。如果浏览器无法克隆表,您可以做的是使用 outerHTML 属性。

e.g.

例如

var tbl = window.opener.table;
try {
    document.getElementById('my_div').appendChild(tbl.cloneNode(true)); 
}
catch (e){
    if (tbl.outerHTML) {
        document.getElementById('my_div').innerHTML = tbl.outerHTML;
    }
    else {
            console.log('not working');
    }
}