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

标签 javascript html

我有一个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/

相关文章:

html - GWT 响应式设计解决方案

javascript - 如何在 JavaScript 中检测声音的频率?

javascript - 多个列表项的同级项

javascript - 缓存图像的 CORS 策略

javascript - 如何使用 &lt;input&gt; 将上传的内容保存到 JavaScript 变量中?

javascript - 向 LIGHTBOX 添加 SWIPE 功能?

html - CSS 使用绝对 div 模糊并保留锐边

html - 底部的可移动导航在窗口调整大小时停留在那里

html - 如何创建嵌套 div 的重复彩虹是 CSS

javascript - 设置一天的 CSS 设计?