Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将输入从父文档传递到iframe_Javascript_Jquery_Html_Dom_Iframe - Fatal编程技术网

Javascript 将输入从父文档传递到iframe

Javascript 将输入从父文档传递到iframe,javascript,jquery,html,dom,iframe,Javascript,Jquery,Html,Dom,Iframe,我在我的项目主页上嵌入了一个iframe,它的src被更改为具有相同输入id的不同HTML模板(id=“name1”),这些源是“first.HTML”和“second.HTML”,并且托管在同一服务器上(不应该存在任何相同的源问题) first.html的代码是: <p>A common computer greeting is: <input type="text" name="display" id="name1"/></p> 不幸的是,当我单击按钮调

我在我的项目主页上嵌入了一个iframe,它的
src
被更改为具有相同输入id的不同HTML模板
(id=“name1”)
,这些源是“first.HTML”和“second.HTML”,并且托管在同一服务器上(不应该存在任何相同的源问题)

first.html的代码是:

<p>A common computer greeting is: <input type="text" name="display" id="name1"/></p>
不幸的是,当我单击按钮调用函数时,iframe消失了

有什么想法吗?如果你有任何问题或想澄清,请告诉我


谢谢你的意见

不要使用
write
它会擦除页面。使用iframe的onload事件回调从父窗口执行任何操作(元素必须存在,才能与它们交互)。通过web搜索
iframe.contentDocument.getElementById(“name1”)。value=…
?,您应该能够找到许多关于与iframe交互的教程和帖子???只是想知道,为什么这个问题被jQuery标记了?
<p>People frequently use the phase: <input type="text" name="display" id="name1"/></p>
Field1: <input type="text" id="field1" value="Hello World!"><br>
<iframe src="about:blank" name="myFrame"></iframe>
<button type="button" onclick="writeToFrame();">Try it!</button>
<script>
function writeToFrame() {
var iframe = window.frames["myFrame"].document;
iframe.open();
iframe.write(document.getElementById("field1").value);
iframe.close();
}
</script>
function writeToFrame() {
var iframe = window.frames["myFrame"].document;
iframe.open();
iframe.write(iframe.getElementById("name1").value = document.getElementById("field1").value);
iframe.close();
}