简体   繁体   English

如何在不使用write()的情况下将iframe文档替换为另一个现有的文档对象

[英]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.

相关问题 使用 Javascript、NodeJS 填写预先存在的 PDF 文档 - Fill out pre-existing PDF document with Javascript, NodeJS 如何在模式中未存在的MongoDB中的现有文档中定义新字段? (Node.js) - How to define a new field in a pre-existing document in MongoDB that isn't in the schema? (Node.js) 有没有办法在不复制的情况下用另一个替换 iframe 的文档? - Is there a way to replace an iframe's document with another without copying? 在不使用document.write的情况下将iframe插入网站 - Insert iframe into site without using document.write 如何确定是否可以将新字段添加到现有对象? - How to be sure if a new field can be added to the pre-existing object or not? 在 Firestore 中创建子集合会生成两个具有相同 id 的文档(而不是更新预先存在的文档) - Creating subcollection in Firestore generates two documents with same id (rather than updating the pre-existing document) 如何注射<script> tag into empty iframe without document.write? - How to inject <script> tag into empty iframe without document.write? 打开没有ID的现有实体 - Opening a pre-existing entity without an ID 如何替换窗口/ iframe的文档对象 - howto replace document object of a window/iframe 如何使用document.write()将对象写入文档 - How To Write Object To Document with document.write()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM