一、獲取頁面元素
取父窗口的元素方法:$(selector, window.parent.document);
那么你取父窗口的父窗口的元素就可以用:$(selector, window.parent.parent.document);
類似的,取其它窗口的方法大同小異
$(selector, window.top.document);
$(selector, window.opener.document);
$(selector, window.top.frames[0].document);
二、子窗口創建
javascript 彈出子窗口
(1) 通過window對象的open()方法,open()方法將會產生一個新的window窗口對象
其用法為:window.open(URL,windowName,parameters);
URL: 描述要打開的窗口的URL地址,如何為空則不打開任何網頁;
windowName:描述被打開的窗口的民稱,可以使用'_top'、'_blank'等內建名稱,這里的名稱跟<a href="..." target="...">里的target屬性是一樣的。
parameters:描述被打開的窗口的參數值,或者說是樣貌,其包括窗口的各個屬性值,及要傳入的參數值。
例如:打開一個400 x 100 的干凈的窗口:
open('','_blank','width=400,height=100,menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes')
也可以這樣寫:var newWindow = open('','_blank');
參數說明如下:
top=# 窗口頂部離開屏幕頂部的像素數
left=# 窗口左端離開屏幕左端的像素數
width=# 窗口的寬度
height=# 窗口的高度
menubar=... 窗口有沒有菜單,取值yes或no
toolbar=... 窗口有沒有工具條,取值yes或no
location=... 窗口有沒有地址欄,取值yes或no
directories=... 窗口有沒有連接區,取值yes或no
scrollbars=... 窗口有沒有滾動條,取值yes或no
status=... 窗口有沒有狀態欄,取值yes或no
resizable=... 窗口給不給調整大小,取值yes或no
三、父窗口與子窗口之間通信
(1) 使用window.open()創建的窗口與父窗口通信
可以在子窗口頁面中通過window.opener來獲取父窗口對象,獲取之后子窗口便可以對父窗口執行刷新,傳值等操作。
如:
window.opener.location.reload(); //子窗口刷新父窗口
window.opener.location.href //獲取父窗口href
window.opener.locaiton.pathname //獲取父窗口路徑名
//刷新父頁面
window.location.href=window.location.href ; //重新定位父頁面
window.location.reload;