admin 管理员组文章数量: 1086019
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).
I've broken down the code to the simplest I can.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
".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>
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.
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?
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).
I've broken down the code to the simplest I can.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3/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>
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.
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?
Share Improve this question edited Feb 18, 2010 at 19:11 aakoch asked Feb 17, 2010 at 20:59 aakochaakoch 1,1741 gold badge9 silver badges18 bronze badges 2- I don't think you'll be able to fix this. – SLaks Commented Feb 17, 2010 at 21:05
- FYI, I found out that it works in IE8. – aakoch Commented Feb 17, 2010 at 21:23
3 Answers
Reset to default 4Your 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"));
or
fragment.appendChild(fragment.ownerDocument.adoptNode(document.createElement("div"));
I'm just hazarding a guess here, but you could try creating the div using
var div = frames[0].document.createElement("div")
instead of
var div = document.createElement("div")
Using the main document's createElement() method may be why IE is having a problem.
I think I found the answer here: http://www.alistapart./articles/crossbrowserscripting/
Importing documents from two different ownerDocument properties ... requires the use of the DOM Level 2 method importNode(), since in these cases the DOM will not allow a simple document.appendChild().
本文标签: javascriptCan39t appendChild to a node created from another frameStack Overflow
版权声明:本文标题:javascript - Can't appendChild to a node created from another frame - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744062050a2526899.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论