0

我试图在子窗口中提醒一个变量(来自父窗口)。我在这里查看了解决方案,但它们对我不起作用,我认为我的代码中缺少某些内容或错误。当我运行代码时,错误控制台告诉我 window.opener 为空。我试过 parent.window.opener... 这也说同样的话。

父.htm:

<head>
<script type="text/javascript">
            var testtest = "hi";

            function reply_click(clicked_id){

                document.getElementById('resultspanel').style.display = 'none';
                document.getElementById('mainpanel').style.display = 'none';            

                var buttonid = clicked_id;
                window.buttonid = buttonid; //makes it global
                alert(buttonid);

                switch(clicked_id) {
                    case "1":
                    case "2":
                    case "3":
                    case "4":
                    case "5":
                    case "6":
                    case "7":
                    case "8":                   
                    case "9":
                    case "10": $('#mainpanel').load("info.htm");document.getElementById('mainpanel').style.display = 'block'; break;    
                }

                    //$('#mydiv').load('newPage.html', {field1: 'value1', ...});
            }


            </script>
</head>

孩子.htm

<head>
<script type="text/javascript">
            function test(){
                //alert(this.parentNode.id);
                //alert(parent.window.opener.testtest);
                //window.opener.alert(testtest);
                var myvar = window.opener.buttonid;
                alert(myvar);
            }
        </script>
</head>
    <body>      
    <p>INFORMATION</p>
        <div id = "test" style = "border-style:solid; height:10%; width:10%;" onClick = "test();" >DIV</div>
    </body>
4

1 回答 1

0

你在某种程度上是对的。window.opener是对打开当前窗口的窗口的引用。但这仅适用于使用window.open函数调用的窗口。它们通常出现在新窗口(或标签)中。

如果需要处理不同的框架,那么他应该使用parent直接祖先的属性,top最顶层的框架和frames直接子级的属性。当您使用frameset而不是iframes 时,还有其他复杂情况(例如带有框架集的窗口没有直接内容)。我希望你不需要它们。

于 2014-05-11T21:07:44.063 回答