如何深度克隆iframe? [英] How to deep clone iframe?

查看:121
本文介绍了如何深度克隆iframe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法深度克隆iframe?

Is there any way to deep clone iframe?

基本的jQuery克隆只是使用相同的src创建另一个iframe。我想要实现的是一种克隆iframe的方法,它是当前的精确内容(即任何可能的输入值,通过javascript修改任何DOM等)。

Basic jQuery cloning just makes another iframe with the same src. What I want to achieve is a way to clone the iframe and it's exact current content (ie. any possible input values, any DOM modifications through javascript etc.).

var clone = iframe.contents().find('html').clone();

当我克隆HTML属性及其所有子项,修改等时,我遇到了以下问题:

When i clone the HTML attribute and all its children's, modifications etc I have these problems:


  1. 我错过了DOCTYPE

  1. I'm missing the DOCTYPE

如何插入它进入新的iFrame?

how do I insert it into the new iFrame?


推荐答案

如果克隆 iframe 创建另一个 iframe 然后你克隆你的 iframe !它的来源是其他地方,当你指定 src 属性时它会被下载。

If you clone the iframe creating another iframe then you deep clone your iframe! It's source is somewhere else and it'll be downloaded when you assign its src attribute.

想象一下,您有一个打印当前时间的源(非缓存)页面。在您的页面上放置 iframe ,等待10秒然后克隆它。新的 iframe 将下载其内容,第二个 iframe 将有不同的内容(时间)。

Imagine you have as source a (non cached) page that prints the current time. Put an iframe on your page, wait 10 seconds then clone it. The new iframe will download its content and the 2nd iframe will have a different content (the time).

编辑:如果您需要深度原始克隆,您可以获取innerHTML并将其注入iframe。来自 Michael Mahemof 的解决方案:

EDIT: if you need a deep raw clone you may get the innerHTML and inject it inside the iframe. Solution from Michael Mahemof:

// Creates the new iframe, add the iframe where you want
var newframe = document.createElement("iframe");
document.body.appendChild(newframe); 

// We need the iframe document object, different browsers different ways
var frameDocument = newFrame.document;
if (newFrame.contentDocument)
 frameDocument = newFrame.contentDocument;
else if (newFrame.contentWindow)
 frameDocument = newFrame.contentWindow.document;

// We open the document of the empty frame and we write desired content.
// originalHtmlContent is a string where you have the source iframe HTML content.
frameDocument.open();
frameDocument.writeln(originalHtmlContent);
frameDocument.close();

这篇关于如何深度克隆iframe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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