1,首先是原始的html文件
<html> <head> <title>open的父子窗口函数演示</title> <script language="javascript" type="text/javascript"> var newWindow;//代表一个新窗口 function test(){ newWindow=window.open("test.html","_blank"); } function test2(){ newWindow.$("info").value=$("info2").value;//向子窗口发送信息 /* newWindow.document.getElementById("info").value=document.getElementById("info2").value; */ } //将document.getElementById封装成函数 function $(id){ return document.getElementById(id); } </script> </head> <body > <input type="button" value="打开一个子窗口" onclick="test()" /><br> <span id="myspan">消息</span><br> <input type="text" id="info2" /><input type="button" value="向子窗口传信息" onclick="test2()"> </body> </html>
2,然后是新开的窗口文件
<script language="javascript" type="text/javascript"> function notify(){ var val=document.getElementById("info").value; alert(val); opener.document.getElementById("myspan").innerText=val; } </script> 这是一个新窗口 <input type="text" id="info" value=""/><br> <input type="button" value="通知给父窗口" onclick="notify()">
3,结果
未使用function $(id):在360浏览器中不兼容,在谷歌和ie浏览器中一切正常
使用function $(id)后所有浏览器都不能正常显示
在看的视频中封装函数可以这样正常使用,不知道是不是浏览器升级后的问题,小白一枚。不喜勿喷,还请多指教