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

发布于 2024-12-04 16:32:34 字数 2369 浏览 0 评论 0 原文

以下代码适用于 IE6+

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);

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

编辑:

<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 上我...

<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 文本框中输入的值。

The following code works on IE6+

thisMessagePreFormat = document.getElementById('child').contentWindow
           .window.document.getElementById('childcont').innerHTML;

I've googled and tried various things...

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);

However I can't access the value from the child script. I know this would normally be a security risk, but in this case it isn't a problem.

EDIT:

<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> Send </nobr></td>
    </tr>
</table>
</html>

On window/bottom.html I have...

<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>

I've try to get the value typed into the childcont textbox in my parent script.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小苏打饼 2024-12-11 16:32:34

你看错了方向。

.innerHTML 更改为 .value

alert(window.frames['child'].document.getElementById('childcont').value);

You're looking at the wrong direction.

Change .innerHTML to .value:

alert(window.frames['child'].document.getElementById('childcont').value);
2024-12-11 16:32:34

我在 FF 中尝试过

 document.getElementById('child').contentWindow.document.getElementById('childcont').innerHTML;

,效果很好。虽然它不是跨域,但它应该可以解决

第四个警报尝试中的错误: contentWindow 是 iframe 的窗口,它没有 window 属性。另外,#childcont 不会有主体,因为它是一个元素,而不是文档

i tried with

 document.getElementById('child').contentWindow.document.getElementById('childcont').innerHTML;

in FF and it works fine. while it is not cross-domain it should work

your error on the fourth alert try is: contentWindow is the window of the iframe, it doesn't have a window property. also, #childcont won't have a body, as it is an element, not a document

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文