这是交易(有点过于简单) 我正在创建一个具有“选项卡”的应用程序,每个选项卡都可以有类似的内容显示为 iFrame。 因此,我希望在选项卡切换之间共享这些内容,因为来自 iFrame 加载的初始请求在某种程度上是一项昂贵的操作。
但是,这需要我“保存”iframe,然后重新插入到 DOM 上。 这有两个问题: 1)我似乎无法访问 iframe 的 contentWindow 来调用 javascript 函数,而它位于内存中 2) 只要将 iframe 插入到 DOM 中,它就会重新加载。
我读到 document.adoptNode 可能会解决问题 #2,所以我尝试了以下操作
<pre><code><html>
<head>
</head>
<body>
<script>
window.onload = function() {
setTimeout(adoptIFrame, 500);
};
var next = ['red', 'blue']
var index = 0;
function adoptIFrame() {
var iFrame = document.adoptNode(document.getElementById("frame"));
document.getElementById(next[index++ % 2]).appendChild(iFrame)
setTimeout(adoptIFrame, 500);
}
</script>
<div id="red" style="width:500;height:500;background-color:red;">
</div>
<div id="blue" style="width:500;height:500;background-color:blue;">
<iframe src="your_url" id="frame"/>
</div>
</body>
</code></pre>
但是每次重新插入时仍然会进行刷新。
有什么想法吗?
最佳答案
这有点晚了,但由于我遇到了同样的问题,我做了一些研究,似乎该功能已从 Webkit 中删除:https://bugs.webkit.org/show_bug.cgi?id=81590 .
关于dom - 尝试在 DOM 中的 iframe 中移动而不刷新。 Chrome 的 "adoptNode"似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10618602/