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/15097986/

相关文章:

javascript - 使用可拖动的 jQuery 函数

javascript - 在 jasmine 的期望中使用 jquery 选择器

javascript - 如何在 jquery 中使用 .animate()

javascript - 如何等待 <object> 中的 SVG 在某些 JS 触发之前完成加载?

javascript - 将 Google 日历作为 iframe 嵌入到 React 应用程序中?

javascript - 通过PHP函数获取<select>属性

Javascript 下拉边框大小和颜色

javascript - 通过 js 停止 youtube-iframe-video

javascript - 返回值 +new Date() 和 Date.now() 有区别吗?

html - Orbeon Form Runner iFrame 问题