window.opener 子窗体操作父窗体

本文提供了一个父窗体通过按钮打开子窗体,并在子窗体中改变父窗体按钮颜色的例子。使用JavaScript实现跨窗口交互功能,但在Chrome浏览器中可能受限。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下是看到的一个例子:

1.父窗体main.html:

 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
 2 <HTML> 
 3 <head>
 4     <script type="text/javascript">
 5         function OpenChildWindow () {
 6             window.open ("sub.html", "OpenerTest", "width=300, height=200");
 7             //window.showModalDialog("sub.html", window);
 8         }
 9     </script>
10 </head>
11 <body>
12     <button id="childOpener" onclick="OpenChildWindow();">Open child window!</button>
13     <br /><br />
14 </body>
15 </HTML>

2.子窗体sub.html:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta charset="utf-8" />
 5     <title>Modeless window example</title>
 6     <script type="text/javascript">
 7         function ChangeOpener (selectTag) {
 8             //if(window.opener == undefined) {
 9             //    alert("undefined");
10             //}
11             var openerWindow = window.opener;
12             var button = openerWindow.document.getElementById ("childOpener");
13             button.style.color = selectTag.options[selectTag.selectedIndex].text;
14         }
15     </script>
16 <body>
17     Change the color of the button in the opener window:
18     <select onchange="ChangeOpener(this)">
19         <option>black</option>
20         <option>red</option>
21         <option>blue</option>
22         <option>green</option>
23     </select>
24 </body>
25 </html>

在chrome下折腾了一阵子,始终没有效果,IE、FF没问题,论坛中看到一位朋友解释如下(未测):

“本地file浏览器测试不要用chrome(webkit核心的浏览器),不能互相操作”。

转载于:https://www.cnblogs.com/hzz521/p/4262995.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值