Javascript,iFrame-如何跨浏览器从另一个脚本的文本框中获取值?
以下代码适用于IE6+Javascript,iFrame-如何跨浏览器从另一个脚本的文本框中获取值?,javascript,internet-explorer,firefox,cross-browser,Javascript,Internet Explorer,Firefox,Cross Browser,以下代码适用于IE6+ thisMessagePreFormat = document.getElementById('child').contentWindow .window.document.getElementById('childcont').innerHTML; 我在谷歌上搜索并尝试了各种各样的东西 alert(window.frames['child'].document.getElementById('childcont').innerHTML); ale
thisMessagePreFormat = document.getElementById('child').contentWindow
.window.document.getElementById('childcont').innerHTML;
我在谷歌上搜索并尝试了各种各样的东西
alert(window.frames['child'].document.getElementById('childcont').innerHTML);
alert(document.frames('child').window.document
.getElementById('childcont').innerHTML);
alert(document.frames('child').document.getElementById('childcont').innerHTML);
alert(document.getElementById('child').contentWindow.window
.document.getElementById('childcont').body.innerHTML);
但是,我无法从子脚本访问该值。我知道这通常是一个安全风险,但在这种情况下,这不是一个问题 编辑:
函数receive(){
警报(document.getElementById('child').contentWindow.document.getElementById('childcont').innerHTML);
警报(“发送消息”);
警报('EE'+document.getElementById('child').contentWindow.document.getElementById('childcont').innerHTML);
警报('US'+document.getElementById('child').contentWindow.window.document.getElementById('childcont').innerHTML);
警报('ME'+document.getElementById('child').contentWindow.document.getElementById('childcont').valueOf);
警报('EE'+document.getElementById('child').contentWindow.document.getElementById('childcont').innerHTML);
//警报('US'+document.getElementById('child').contentWindow.window.document.getElementById('childcont').innerHTML);
}
发送
在window/bottom.html上我有
<html><head></head>
<body topmargin="0" rightmargin="0" bottommargin="0" leftmargin="0">
<table cellpadding="0" cellspacing="0" style="border:1px solid gray;">
<tr><td>
<textarea id="childcont"></textarea>
</td></tr></table>
</body>
</html>
我已尝试在父脚本的childcont文本框中键入值。我尝试使用
document.getElementById('child').contentWindow.document.getElementById('childcont').innerHTML;
在FF中,它工作正常。虽然它不是跨域的,但应该可以工作
您在第四次警报尝试中的错误是:contentWindow是iframe的窗口,它没有window属性。另外,#childcont没有主体,因为它是一个元素,而不是一个文档您看错了方向 将
.innerHTML
更改为.value
:
alert(window.frames['child'].document.getElementById('childcont').value);
“我知道这通常是一个安全风险,但在这种情况下这不是一个问题。”这意味着什么?如果它是跨域的,它将不起作用。您的错误是什么?在尝试获取元素之前,框架是否已完全加载?
alert(window.frames['child'].document.getElementById('childcont').value);