首页 > 代码库 > window.opener 子窗体操作父窗体

window.opener 子窗体操作父窗体

以下是看到的一个例子:

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核心的浏览器),不能互相操作”。

window.opener 子窗体操作父窗体