【IT168技术文档】
不能使用window.parent
Window.parent是用来在frame中进行操作的,在对话框中不能用来操作父窗口对象
正确的做法
调用modaldialog时通过传参数的方式操作
例:
需求
父窗口页面为a.html 子窗口页面为b.html。a.html中有文本框id为test1,在打开的对话框中点击按钮,将a.html的文本框值改为“子窗口值”。
实现
打开对话框时把test1作为参数传给子窗口,在子窗口中获取参数,将参数对象(即a.html中传过来的text对象)的value属性值设置为“子窗口值”
注意:这里只能传id,不能传name
a.html代码如下
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>a.html</title> </head> <body> <input type=text id=test1 value=''> <input type=button value=" OK " onclick='window.showModalDialog("b.html", test1)'> </body> </html> b.html代码如下 <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <title>b.html</title> <script language=javascript> function func1(){ //获取父窗口传过来的参数 var ptextid = window.dialogArguments; if(ptextid != undefined){ //将父窗口传过来的对象的值改为“子窗口值” ptextid.value = "子窗口值"; //关闭子窗口 window.close(); } } </script> </head> <body> <input type=button value=" OK " onclick=func1()> </body> </html>