我有一个main.html
,里面是<ifram id='if' name='if'>
,它会加载另一个网站(这会加载不在我的域中的网站,例如 Facebook)。
在 iframe
中加载的站点有<form name='form2' id='form2'>
,其中有<input type='text' name='tb' id='tb'>
也在 main.html
,我有一个按钮,我想知道我们是否可以在按钮上放置一些 Javascript,以便当我们单击它时,文本框中的文本名为 tb
更改为“az”。
我们怎样才能做到这一点?
最佳答案
您可以这样做,因为这将是跨站点脚本编写,出于安全原因这是被禁止的。您可以使用代理,在 iframe
中提供 HTML通过您自己的网站,因此从浏览器的 Angular 来看它不再是跨网站的。
如果<iframe>
来自同一域,可以轻松访问这些元素:
$("#iFrame").contents().find("#someDiv")
测试:http://jsfiddle.net/Yy6tu/按Ctrl+Shift+j
看看你得到了什么!
更新:如果您有 other.html
将文件放在同一文件夹中,您可以执行以下操作:
main.html:
<head>
$('document').ready(function(){
$("#if").contents().find("#b").val('lalalala');
});
</head>
<body>
<input type='button' name='executer' value="Maro">
<iframe width="600" height="400" src='other.html' name='if' id="if"></iframe>
</body>
other.html:
<form action="x" name="form2" id="form2">
<input type="text" name="tb" id="b">
</form>
这应该有效!
关于javascript - 如何写入 iframe 内的文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14723426/