Home >Web Front-end >JS Tutorial >Parse jquery to get the elements of the parent window_jquery

Parse jquery to get the elements of the parent window_jquery

WBOY
WBOYOriginal
2016-05-16 17:31:12886browse

("#父窗口元素ID",window.parent.document); 对应javascript版本为window.parent.document.getElementByIdx_x("父窗口元素ID");

取父窗口的元素方法:$(selector, window.parent.document);
那么你取父窗口的父窗口的元素就可以用:$(selector, window.parent.parent.document);
类似的,取其它窗口的方法大同小异
$(selector, window.top.document);
$(selector, window.opener.document);
$(selector, window.top.frames[0].document);

-----------------------------------------------------------------------------
子窗口创建及父窗口与子窗口之间通信:
 
1、Javascript弹出子窗口
可以通过多种方式实现,下面介绍几种方法
(1) 通过window对象的open()方法,open()方法将会产生一个新的window窗口对象
其用法为:
window.open(URL,windowName,parameters);
URL: 描述要打开的窗口的URL地址,如何为空则不打开任何网页;
windowName:描述被打开的窗口的民称,可以使用'_top'、'_blank'等内建名称,这里的名称跟里的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

(2) 在javascript中除了通过open()方法建立window对象实现弹出窗口外,还可以通过建立对话框的方式弹出窗口。
如:
alert(""); //弹出信息提示对话框
confirm(""); //弹出信息确认对话框
prompt(""); //具有交互性质的对话框
但是,上述实现的弹出窗口具有的功能较为单一,只能完成较为简单的功能。对于需要在对话框中显示多个数据信息,
甚至是HTML控件就无能为力了。

(3) 使用模态对话框实现复杂的对话框需求
在javascript的内建方法中还有一类方法可以实现通过对话框显示HTML内容,
也就是说可以通过创建对话框的方式来完成创建窗口对象所能完成的功能。
包括创建模态对话框和非模态对话框两种。

实现方法为:
//创建模态你对话框
window.showModalDialog(sURL,vArguments,sFeatures)
//创建非模态对话框
window.showModelessDialog(sURL,vArguments,sFeatures)

其区别在于:
用showModelessDialog()打开窗口时,不必用window.close()去关闭它,当以非模态方式[IE5]打开时, 打开对话框
的窗口仍可以进行其他的操作,即对话框不总是最上面的焦点,当打开它的窗口URL改变时,它自动关闭。而模态[IE4]方式的对话框始终有焦点(焦点不可移走,直到它关闭)。模态对话框和打开它的窗口相联系,因此我们打开另外的窗口时,他们的链接关系依然保存,并且隐藏在活动窗口的下面。 showModeDialog()则不然。

参数说明:
sURL:必选参数,类型:字符串。
用来指定对话框要显示的文档的URL。
vArguments:可选参数,类型:变体。
用来向对话框传递参数。传递的参数类型不限,包括数组等。对话框通过window.dialogArguments来取得传递进来的参数。
sFeatures:选参数,类型:字符串。
用来描述对话框的外观等信息,可以使用以下的一个或几个,用分号“;”隔开。
dialogHeight:对话框高度
不小于100px,IE4中dialogHeight和dialogWidth 默认的单位是em,而IE5中是px,为方便其见,在定义modal方式的对话框时,用px做单位。
dialogWidth: 对话框宽度。
dialogLeft: 距离桌面左的距离。
dialogTop: 离桌面上的距离。
center: 窗口是否居中
默认yes,但仍可以指定高度和宽度,取值范围{yes | no | 1 | 0 }。
help: 是否显示帮助按钮
默认yes,取值范围 {yes | no | 1 | 0 }。
 resizable: 是否可被改变大小。
默认no,取值范围 {yes | no | 1 | 0 } [IE5+]。
 status: 是否显示状态栏。
默认为yes[ Modeless]或no[Modal],
取值范围{yes | no | 1 | 0 } [IE5+]。
scroll:指明对话框是否显示滚动条。
默认为yes,取值范围{ yes | no | 1 | 0 | on | off }。
还有几个属性是用在HTA中的,在一般的网页中一般不使用。
dialogHide:在打印或者打印预览时对话框是否隐藏。
默认为no,取值范围{ yes | no | 1 | 0 | on | off }。
edge:指明对话框的边框样式。
默认为raised,取值范围{ sunken | raised }。
unadorned:默认为no,取值范围{ yes | no | 1 | 0 | on | off }。

Incoming parameters:
To pass parameters to the dialog box, they are passed through vArguments. There is no limit on the type. For string types, the maximum length is 4096 characters. You can also pass the object
for example:
var newWin=window.showModalDialog(url,window,'dialogHeight:500px, dialogLeft:100px, dialogTop:100px,
dialogWidth:300px, status :0, edge:sunken');
newWin.open();
Compared with using the window.open() method to create a window, the difference between creating a window using the modal method is that after the window is created using the modal method The parent window will not be able to be operated.
2. Communication between the child window and the parent window
(1) The window created using window.open() communicates with the parent window
You can obtain the parent window object through window.opener in the child window page. After obtaining it, the child window can perform operations such as refreshing and passing values ​​to the parent window.
For example:
window.opener.location.reload(); //The child window refreshes the parent window
window.opener.location.href //Gets the parent window href
window.opener.locaiton.pathname //Get the parent window path name
//Refresh the parent page
window.location.href=window.location.href; //Relocate the parent page
window.location. reload;
(2) Modal window communicates with the parent window
When the child window created by using the showModelDialog() and showModelessDialog() methods wants to communicate with the parent window, it cannot communicate with the parent window. opener
to get the parent window object. To achieve communication, the parent window object must be passed to the child window when creating the modal child window.
The implementation is:
In the parent window:
var newWin=window.showModelDialog(url,window,'');
newWin.open();
At this time, the parameter window is the parent window object
In the child window:
You need to get the parent window first object before the parent window object can be used. Since the parent window object is passed in by passing parameters when creating the
child window, the parent window object can only be obtained in the child window by obtaining window parameters. The acquisition method is as follows:
var parent=widnow.dialogArguments;
The variable parent is the parent window object.
For example:
//Submit the form in the parent window through the child window: form1, and execute the query operation after submission
var parent=window.dialogArguments;
parent.document. form1.action="QueryInfor.jsp";
parent.submit();
//Refresh the parent page
var parent=window.dialogArguments;
parent.location.reload();
//Transfer the value from the child window to the parent window
To transfer the value to the parent window in the modal child window, you need to use window.returnValue to complete
The implementation method is as follows:
In the child window:
//Get the value of a field in the parent window, add one to the value and return to the parent window
var parent=window.dialogArguments;
var x=parent. docuement.getElementById("age").value;
x=x 1;
//Return the x value
window.returnValue=x;
in the parent window :
//Get the value from the child window
var newWin=window.showModelDialog(url,window,'');
if(newWin!=null)
document .getElementByIdx_x("age").value=newWin;
//Set the value of the parent window in the child window
It seems that passing the value from the child window to the parent window does not directly set the value in the parent window. Clearly. It is more flexible to directly set the value of the element in the parent window. However, which method to use depends on the actual situation and the existing implementation method, because if an unrealistic method is used, it will not only reduce the development efficiency, but also reduce the cost. Execution efficiency often leads to inelegant implementation methods and coding styles.
The child window sets the value of the parent window as follows:
In the child window:
var parent=window.dialogArguments;
var x=parent .document.getElementByIdx_x("age").value;
x=x 1;
//Set the age attribute value in the parent window
parent.document.getElementByIdx_x("age").value=x;
The above are some methods and information I collected and accumulated when using JavaScript to solve the sub-window problem in the project. I implemented it by creating a modal window (this is mainly related to the project itself), but in fact, whether using window.open() or window.showModelDialog() for parameter passing and other operations, although there are some implementation methods There is a big difference. It may feel a bit confusing at first contact, but once you clarify the relationship and roles between the child window and the parent window, it will be easy to understand.