[英]How can I pass value to iFrame with javascript?
我在页面中打开了一个iFrame,onClick事件尝试将值发送到iframe的文本框
这是我的JavaScript代码
<script type="text/javascript">
function TextToFrame()
{
var frame = frames['frame1'];
frame.document.getElementById("u").value = "wallace";
}
</script>
IFrame会加载页面,但是当我单击Button时,它不会发送任何值。 刷新主页和iFrame
<asp:Button ID="Button1" runat="server" OnClientClick="TextToFrame();" Text="Send Value" />
<IFRAME id="frame1" name="frame1" src="http://tweakers.net/my.tnet/login?location=http%3A%2F%2Ftweakers.net%2F" width=500px height=500px runat="server">
我究竟做错了什么? 任何帮助表示赞赏
我认为您无法在其他域的iframe中修改HTML。 您可能可以将iframe源更改为以下内容:
http://tweakers.net/my.tnet/login?u=wallace
但该网站应为您提供类似的选择。
有一个解决方案:
document.getElementsByName('frame1')[0].contentWindow.getElementById("u").value = "wallace";
您无法在src
属性与其所在域不同的iframe中操纵数据。
它不允许您跨域使用。
看看这个- 同源政策
我同意Haroldis的观点,但还有一点:
<script type="text/javascript">
function TextToFrame()
{
document.getElementsByName('frame1')[0].contentWindow.getElementById("u").value = "wallace";
return false; // For No Post Back in the container Page
}
</script>
和按钮:
<asp:Button ID="Button1" runat="server" OnClientClick="return TextToFrame();" Text="Send Value" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.