无法将appildChild附加到从另一个帧创建的节点 [英] Can't appendChild to a node created from another frame

查看:207
本文介绍了无法将appildChild附加到从另一个帧创建的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有iframe的页面,并希望从子帧中提取一个DOM节点,并将其放在父页面上。这可以在Firefox(3.5)中使用,但不能在Internet Explorer(7)中使用。

I have a page with an iframe and would like to extract a DOM node from the child frame and put it on the parent page. This works in Firefox (3.5), but not in Internet Explorer (7).

我将代码细分为最简单的方法。 b
$ b

I've broken down the code to the simplest I can.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<title>Fragment</title>
</head>

<body>

<iframe src="blank.html"></iframe>

<script type="text/javascript">
window.onload = function () {
    var fragment = document.createDocumentFragment();
    var div = frames[0].document.createElement("div");
    fragment.appendChild(div);
};
</script>

</body>
</html>

我收到错误无效参数 fragment.appendChild(div); 行上。这个错误似乎源于我从iframe的文档和父文档中的div元素创建文档片断的事实。如果两者都使用相同的文档,此代码可以工作。

I get an error "Invalid argument" on the "fragment.appendChild(div);" line. The error seems to stem from the fact that I'm creating the document fragment from the iframe's document and the div element from the parent document. This code works if both use the same document.

我想保留可能附加到DOM节点的任何事件,所以我不想使用innerHTML。

I want to keep any events that might be attached to the DOM nodes, so I don't want to use innerHTML.

任何人都知道这个修复?

Anyone know a fix for this?

推荐答案

你的问题是您没有将节点引入到当前文档中创建的片段中。使用以下方法:

Your problem is that you are not adopting the nodes into the fragment that are created in the current document. Use either the following:

fragment.appendChild(fragment.ownerDocument.createElement("div"));

fragment.appendChild(fragment.ownerDocument.adoptNode(document.createElement("div"));

这篇关于无法将appildChild附加到从另一个帧创建的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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