简体   繁体   English

有没有办法写入通过Iframe显示的外部HTML文件?

[英]Is there a way to write to an external HTML file displayed through an Iframe?

Overall, I just want to write to a external file. 总的来说,我只想写一个外部文件。 So, I have two HTML files and currently one JS file. 所以,我有两个HTML文件,目前有一个JS文件。 Suppose the html pages are called main.html and inframe.html. 假设html页面名为main.html和inframe.html。 Currently, my JavaScript file, is empty, function I am trying to write a function so that if I type something to an input on main.html, click a button, it can be written to inframe.html. 目前,我的JavaScript文件是空的,函数我正在尝试编写一个函数,这样如果我在main.html上的输入中键入内容,单击一个按钮,就可以将其写入inframe.html。

At the moment, I have the iframe: 目前,我有iframe:

<iframe src="inframe.html" style="background: #FFFFFF;"width="350px" height="100%">
        <p>Your browser does not support iframes.</p>
</iframe>

And just a button in main.html: 而只是main.html中的一个按钮:

<button style="width:100%; onclick="doStuff()"> &lt;h4&gt; </button>

The function doStuff() is supposed to simply write "Hello World!" 函数doStuff()应该只写“Hello World!” to inframe.html which in tern should display in the iframe. 到inframe.html应该在iframe中显示哪些内容。

How exactly can I do such a task? 我怎么能完成这样的任务? It seems simple, but I can't figure it out. 看起来很简单,但我无法弄清楚。

One way of doing this is to use the iframe element's contentWindow property, but first give the iframe an id to access it with. 一种方法是使用iframe元素的contentWindow属性,但首先给iframe一个id来访问它。

HTML HTML

<iframe id="myFrame" height="100" width="300" src="about:blank"></iframe>
<button onclick="doStuff()" type="button">doStuff</button>

JavaScript JavaScript的

function doStuff() {
   var frame = document.getElementById("myFrame");
   var win = frame.contentWindow;

   win.document.write("hello folks")
}

Of course document.write is mostly used these days while learning JavaScipt and HTML, and does not see much use elsewhere. 当然, document.write主要用于这些天一边学习javascipt的和HTML,并没有看到太多其他地方使用。

You can research things like this by searching for "MDN IFRAME element" to start with. 您可以通过搜索“MDN IFRAME元素”来开始研究这样的事情。

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

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