简体   繁体   English

如何克隆实际的iFrame内容,而不仅仅是iFrame标签内容?

[英]How to clone actual iFrame contents, not just the iFrame tag contents?

I have an iFrame and have worked out how to clone it using jQuery: 我有一个iFrame,并且已经解决了如何使用jQuery克隆它的方法:

<HTML>
<HEAD>
  <SCRIPT type="text/javascript" src="jquery.js"></SCRIPT>
  <SCRIPT>
    function clone_test() {
      $('#FRAME1').clone().appendTo('#divtag');
    }
  </SCRIPT>
</HEAD>

<DIV id="divtag">
  <iframe id="FRAME1" name="FRAME1" src="http://currencies.typeit.org/" width="360" height="360" frameborder="0"></iframe>
</DIV>

<a href="javascript:clone_test()" target="FRAME1">- CLONE THE iFRAME -</a>

</HTML>

But this is not what I want! 但这不是我想要的! My problem is that anything typed in the currency box is not included in the cloning process. 我的问题是,在克隆过程中不包括在货币框中键入的任何内容。 It seems only the HTML is cloned, not the actual contents of the iFrame. 似乎仅克隆了HTML,而不克隆了iFrame的实际内容。 Is there anyway possible to clone the iFrame including anything typed in the currency box? 无论如何,是否可以克隆iFrame(包括在货币框中键入的任何内容)? This only has to work for the Chrome browser as I am making a Chrome extension. 这仅适用于Chrome浏览器,因为我正在制作Chrome扩展程序。

如果Iframe来自其他域,则由于“相同来源策略”,您将无法访问其内容。

I don't think this is possible, your code just duplicates iframe element, which loads external page again, co it's in "default" state. 我认为这是不可能的,您的代码只是复制了iframe元素,它再次加载了外部页面,处于“默认”状态。 But you cannot access content of iframe. 但是您无法访问iframe的内容。

This cant be done when the url of the iframe and the url of your domain are not the same. 如果iframe的网址和您的域的网址不同,则无法完成此操作。

If they are the same you need to clone the document element of the iframe as well and append it to the cloned iframe. 如果它们相同,则还需要克隆iframe的document元素并将其附加到克隆的iframe中。

There is a workaround for this though. 但是,有一种解决方法。 Due to Same Origin Policy you need to use curl (if you are using php) to "fake" that the content is coming from the same domain. 由于同源政策的缘故,您需要使用curl(如果使用的是php)来“伪造”内容来自同一域。 After which, you may be able to get the content of the iframe by using. 之后,您也许可以通过使用获得iframe的内容。

$("#frameid").contents().find("body *"); $(“#frameid”)。contents()。find(“ body *”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM