Javascript - 使用iframe-in Value填充输入 [英] Javascript - Fill input with iframe-in Value

查看:165
本文介绍了Javascript - 使用iframe-in Value填充输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用iframe-in Value填充输入。

我有两个html文件。 1.html和2.html

第一个(1.html)是这样的:

I wanna fill input with my iframe-in Value.
i have two html file. 1.html and 2.html
First one (1.html) is like that:

<html>
<head>
<title>IFRAME TEST</title>
</head>
<body>
<input type="text" value="" id="mylovelytextbox"><br />
<iframe src="2.html" id="mylovelyiframe">
</body>
</html>

第二个(2.html)就是这样:

And second one (2.html) is like that:

<html>
<head>
<form method="get" action="">
<input type="hidden" name="thisone" value="mylovelychangedvalue">
</form>
</body>
</html>

我想在1.html中用myone的值填充mylovelytextbox。
我该怎么做?
-Sorry for bad english :(

I wanna fill mylovelytextbox in 1.html with thisone's value. How can i do that? -Sorry for bad english :(

编辑:

我用@stano的代码运行代码帮助:)现在,我的代码是这样的:

I run the code with @stano's help :) Now, my code is like that:

<html> <head> <title>IFRAME TEST</title> </head> <body> <input type="text" value="" id="mylovelytextbox"><input type="button" onClick="var iframe = document.getElementById('mylovelyiframe');console.log(iframe,doc,el);var doc = iframe.contentDocument || iframe.contentWindow.document;var el = document.getElementById('mylovelytextbox');el.value = doc.getElementsByName('thisone')[0].value;" value="Click"><br /> <iframe sandbox="allow-same-origin allow-scripts" src="2.html" id="mylovelyiframe"></iframe> </body> </html>

非常感谢,Stano!

Thanks a lot, Stano!

推荐答案

首先修复那些丢失的标签

At first fix those missing tags

<iframe src="2.html" id="mylovelyiframe"></iframe>
</head><body>

并添加一些doctype,然后你可以使用

and add some doctype, then you can use

<script type="text/javascript">
var iframe = document.getElementById('mylovelyiframe');
var doc = iframe.contentDocument || iframe.contentWindow.document;
var elem = document.getElementById('mylovelytextbox');
elem.value = doc.getElementsByName('thisone')[0].value;
</script>

这篇关于Javascript - 使用iframe-in Value填充输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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