<!--主頁面的代碼-->
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script type="text/javascript" src="../../commonJS/jquery.js"></script> <script> </script> </head> <body> <div style="width:140px; height:60px; position:relative;display:inline-block;background-color: red; margin-right:20px;display:inline-block;cursor: pointer;" id="addMatchSchedule"> </div> </body> </html> <script> $("#addMatchSchedule").click(function() {alert("a"); window.open('child.html',"新增","width=500,height=480,screenX=400,screenY=100"); } ); //子頁面調用的函數 window.BBBB = function(){ alert(112); } </script>
<!--子頁面-->
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <script src="../../commonJS/jquery.js"></script> <body> <a class="btn btn-small btn-info" onclick="addSchduleItem();" title="確定" >確定</a> </body> </html> <script> //確定關閉頁面 function addSchduleItem() { if (window.opener != null && !window.opener.closed) { window.opener.BBBB();//調用主頁面的函數 } window.close(true); } </script>
一句話概括思路:在父窗口中打開子窗口,在子窗口中調用父窗口的方法
核心方法:window.open() (方法介紹在本文尾部)
核心概念:window.opener (方法介紹在本文尾部)
window.open()簡介(以具體情況為例):
window.open('page.html', 'newwindow', 'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no') //該句寫成一行代碼
參數解釋:
window.open 彈出新窗口的命令;
'page.html' 彈出窗口的文件名;
'newwindow' 彈出窗口的名字(不是文件名),非必須,可用空''代替;
height=100 窗口高度;
width=400 窗口寬度;
top=0 窗口距離屏幕上方的象素值;
left=0 窗口距離屏幕左側的象素值;
toolbar=no 是否顯示工具欄,yes為顯示;
menubar,scrollbars 表示菜單欄和滾動欄。
resizable=no 是否允許改變窗口大小,yes為允許;
location=no 是否顯示地址欄,yes為允許;
status=no 是否顯示狀態欄內的信息(通常是文件已經打開),yes為允許;
window.opener 簡介
window.opener 實際上就是通過window.open打開的子窗體的父窗體