javascript - 使用 Javascript 或 jQuery 将元素写入子 iframe

标签 javascript jquery iframe

我有这样的东西:

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

我想使用 jQuery 编写元素,这样完整的等效 HTML 将如下所示:

<!doctype html>
<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 都可以。

谢谢。

编辑:经过更多的研究,我似乎正在寻找 iframe 的 contentDocument 属性的 IE 等价物。 “contentDocument”是FF支持的W3C标准,但IE不支持。 (惊喜惊喜)

最佳答案

你可以两者都做,你只需要不同的目标:

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 - 使用 Javascript 或 jQuery 将元素写入子 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/997986/

相关文章:

javascript - 将 div 移动到位于 iframe 内的另一个 div

cordova - 在 Cordova 上保护 postMessage()

Javascript (ES8) - 从父类获取类的静态值

javascript - IndexOf 无法正确处理包含数组的对象

jquery - 如果在 .ready 上选中复选框,则添加Class

javascript - Jquery flot 插件不根据日期绘制图形

jquery - Bootstrap Glyphicons datetimepicker 更改图标

javascript - 如果输入与数组名称不匹配则显示 "no results"(javascript)

javascript - Jquery 动画动态宽度?

javascript - 在选项卡更改时动态更改 iframe 的高度?