[英]How to replace iframe document with another pre-existing document object, without using write()
I just want to replace a blank iframe's document with a pre-existing document object. 我只想用一个预先存在的文档对象替换一个空白的iframe文档。 Not a string of HTML that can use the
write()
method with (that solution is in many other answers, here) . 不是可以与
write()
方法一起使用的HTML字符串(该解决方案在许多其他答案中) 。 I just want a document object to replace another document. 我只希望一个文档对象替换另一个文档。
My blank iframe: 我的空白iframe:
<iframe src="about:blank" id="myIframe"></iframe>
Routine to replace blank iframe document: 替换空白iframe文档的例程:
function replaceIframeDoc(objDoc){
var nod_if = document.getElementById("myIframe");
var nod_ifDoc = nod_if.contentWindow.document || nod_if.contentDocument;
nod_ifDoc = objDoc;
}
Document object created, and call to do the replace: 创建文档对象,并调用执行替换:
(Note: I encapsulate my document object in a simple object... I need to do that for some other unrelated stuff... just to easily transfer some attributes, etc. Hopefully it's inconsequential to my issue!) (注意:我将我的文档对象封装在一个简单的对象中……我需要对其他一些不相关的东西进行此处理……只是为了轻松地传递一些属性,等等。希望这对我的问题无关紧要!)
var obj_container = new Object; //just to make easier to do some other stuff
obj_container.document = document.implementation.createHTMLDocument();
obj_container.document.open();
obj_container.document.write("<html>...(some html)...</html>");
obj_container.document.close();
replaceIframeDoc(obj_container.document);
Depending on which browser you need to support, you could do something like this: 根据您需要支持的浏览器,您可以执行以下操作:
var iframe = document.getElementById("myIframe");;
var oldNode = iframe.contentDocument.getElementById("myNode");
var newNode = document.importNode(oldNode, true);
document.getElementById("container").appendChild(newNode);
You can read more here: https://developer.mozilla.org/en-US/docs/Web/API/document.importNode 您可以在此处阅读更多信息: https : //developer.mozilla.org/zh-CN/docs/Web/API/document.importNode
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.