js之open子父窗口传参(以及调用父页面方法等)

父页面html代码

<!
DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script> var arr = new Array(); arr[0] = "test1"; arr[1] = "test2"; function openUrl() { var url = 'openChild.html'; var opts = 'height=400,width=800,left=200,top=200,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no'; window.open(url, "myChildWin", opts); } function setArr(value) { arr[2] = value; } function setDiv(v) { document.getElementById("mydiv").innerHTML = v; } </script> </head> <body> <input type='button' onclick=openUrl() name='test' value="click me "> <div id="mydiv"></div> <div id="mydiv2"></div> </body> </html>


子页面html代码

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title></title>
        <script type="text/javascript" src="js/jquery.js" ></script>
        <script>
            function test() {
                window.opener.setArr("test3");
                var data = window.opener.arr;
                document.write("成功获取父窗口定义的数据====" + data);
                window.opener.setDiv("成功修改父窗口的div值llllll");
                
                //window.opener.document.getElementById("mydiv2").innerHTML="<font style='red'>通过opener.doucment直接修改</font>";
                //window.opener.document.getElementById("mydiv2").style.color="yellow"; //可以生效 ,其中把opener改为parent后失效
               $("#mydiv", window.opener.document).css("color","red"); //可以生效,其中把opener改为parent后失效
            }
        </script>
    </head>

    <body onload="test()">
        我是子窗口
    </body>

</html>
 
  
 
 

猜你喜欢

转载自www.cnblogs.com/rdchen/p/13183377.html