gpt4 book ai didi

javascript - 如何写入iframe内的文本框

转载 作者:行者123 更新时间:2023-11-27 22:34:46 24 4
gpt4 key购买 nike

我有一个 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文件放在同一个文件夹中,您可以这样做:

ma​​in.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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com