首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >iFrame -如何跨浏览器,从另一个脚本的文本框中获取值?

iFrame -如何跨浏览器,从另一个脚本的文本框中获取值?
EN

Stack Overflow用户
提问于 2011-09-12 23:53:24
回答 2查看 971关注 0票数 0

以下代码可以在IE6+上运行

代码语言:javascript
运行
复制
thisMessagePreFormat = document.getElementById('child').contentWindow
           .window.document.getElementById('childcont').innerHTML;

我在谷歌上搜索过并尝试过各种方法...

代码语言:javascript
运行
复制
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);

但是,我不能从子脚本访问该值。我知道这通常会有安全风险,但在这种情况下,这不是问题。

编辑:

代码语言:javascript
运行
复制
<html><head></head>
<script language="JavaScript">
    function receive() {
        alert(document.getElementById('child').contentWindow.document.getElementById('childcont').innerHTML);
        alert('Send Message');
        alert('EE ' + document.getElementById('child').contentWindow.document.getElementById('childcont').innerHTML);
        alert('US ' + document.getElementById('child').contentWindow.window.document.getElementById('childcont').innerHTML);
        alert('ME ' + document.getElementById('child').contentWindow.document.getElementById('childcont').valueOf);
        alert('EE ' + document.getElementById('child').contentWindow.document.getElementById('childcont').innerHTML);
        //alert('US ' + document.getElementById('child').contentWindow.window.document.getElementById('childcont').innerHTML);
    }
</script>

<iframe  id="child" name="child" src="window/bottom.html" ></iframe>

<table style="CURSOR: pointer; display: inline;" cellpadding="0" cellspacing="0" onClick="javascript:receive();">
    <tr><td height="17"><nobr>&nbsp;Send&nbsp;</nobr></td>
    </tr>
</table>
</html>

在window/bottom.html上我有...

代码语言:javascript
运行
复制
<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>

我已经尝试将值输入到父脚本中的子对象文本框中。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-13 22:13:05

你看错方向了。

.innerHTML更改为.value

代码语言:javascript
运行
复制
alert(window.frames['child'].document.getElementById('childcont').value);
票数 0
EN

Stack Overflow用户

发布于 2011-09-13 00:05:54

我试着用

代码语言:javascript
运行
复制
 document.getElementById('child').contentWindow.document.getElementById('childcont').innerHTML;

在FF中,它运行良好。虽然它不是跨域的,但它应该可以工作

您在第四次警告尝试时的错误是: contentWindow是iframe的窗口,它没有窗口属性。此外,#childcont不会有主体,因为它是一个元素,而不是文档

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7390602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档