使用Javascript或jQuery将元素写入子iframe [英] Write elements into a child iframe using Javascript or jQuery

查看:147
本文介绍了使用Javascript或jQuery将元素写入子iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的事情:

<html>
  <body>
     <iframe id="someFrame"></iframe>
  </body>
</html>

我想使用jQuery编写元素,使得完全等效的HTML将是这样的:

And I would like to use jQuery to write elements such that the full equivalent HTML would be like this:

<html>
  <body>
    <iframe id="someFrame">
      <!-- inside the iframe's content -->
      <!-- <html><body>  -->
      <div>A</div>
      <div>B</div>
      <div>C</div>
      <!-- </body></html> -->
    </iframe>
  </body>
</html>

或者,任何普通的Javascript都没问题。

Alternatively, any plain-old-Javascript would be fine.

谢谢。

编辑:经过一番研究后,似乎我正在寻找与IE相当的IE iframe的contentDocument属性。 contentDocument是FF支持的W3C标准,但IE不支持。 (惊喜)

Edit: After a little more research, it seems I am looking for an IE-equivalent of the contentDocument property of an iframe. "contentDocument" is a W3C standard which FF supports, but IE does not. (surprise surprise)

推荐答案

你可以做到这两点,你只需要以不同的方式定位:

You can do both, you just have to target differently:

var ifrm = document.getElementById('myIframe');
ifrm = ifrm.contentWindow || ifrm.contentDocument.document || ifrm.contentDocument;
ifrm.document.open();
ifrm.document.write('Hello World!');
ifrm.document.close();

这篇关于使用Javascript或jQuery将元素写入子iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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