子窗体与父窗体的通讯

父窗体与子窗体通讯
A通过window.open()方式打开B,B如何在A的下拉列表框中增加一个选项????
 window.opener.document.getElementbyId("select").options.add(new Option("display","value"))

window.opener  返回的是对父窗体的一个引用,通过它可以控制父窗体,常见有:

1、设置父页面背景
window.opener.document.body.backgroundColor = "red" ;

2、刷新父页面
window.opener.location.reload();

3、设置父页面文本框的值
window.opener.document.getElementById("txtUserName").value = "张三" ;

4、设置父页面单选按钮的状态
window.opener.document.getElementById("rdoMan").checked = true ;

5、显示父页面的层(利用层的透明属性可以制作蒙板效果)
window.opener.document.getElementById("divMask").style.display = "block" ;

6、让父页面应用滤镜效果  -- 应用灰度滤镜
window.opener.document.body.style.filter = "gray()"

function addOptions(){
 //构造一个option选项
 var op = new Option("新浪","www.sina.com.cn");
 //自己窗口的select选项中再添加一项
window.document.getElementById("newUrl").options.add(op);
  
 //子窗体获得父窗体中定义的变量
 //alert(window.opener.obj);

 //子窗体为父窗体的文本框赋值
window.opener.document.getElementById("username").value="accp";
 //子窗体控制父窗体中表单的单选按钮  window.opener.document.getElementById("man").checked=true;

 //子窗体控制父窗体中表单的下拉列表框  
  //window.opener.documenet.getElementById("myUrl").options.length = 0;  //window.opener.document.getElementById("myUrl").options[i] = op ;
 //????????????子窗体怎样取得父窗体中的下拉列表框的选项
 //答案就是通过window.opener.document创建节点的方式来解决。


 //创建一个父窗体的Option节点
 var op2 = window.opener.document.createElement("option");
 //创建一个父窗体的文本节点
 var txtNode = window.opener.document.createTextNode("新浪");
 //为父窗体的Option节点添加属性
 op2.setAttribute("value","www.sina.com.cn");
 //将父窗体的文本节点做为子节点添加到Option节点上
 op2.appendChild(txtNode);
 //最后将创建好的Option节点添加到父窗体的options集合中  window.opener.document.getElementById("myUrl").options.appendChild(op2) ;
 
 //暂停后面的关闭自己操作,观看父窗口的变化
 alert();

其实在JS中认为父窗体与子窗体优先级不一样,子窗体不用将opener置空也能无弹框关闭自己窗口,但是父窗体认为有很重要内容,需要客户确定,所以就提示如果将opener置空,则优先级变为普通窗休,没有父窗体,关闭时不用弹框建议将opener置空,否则opener在达到一定数量后浏览器才清空。

 window.opener = null ;
 self.close();
}

注意:如果是showModalDialog方法打开的子窗口,控制父窗体应该这样做:
window.showModalDialog('default2.aspx',  
 {doc:document,win:parent},
 '...');
dialogArguments.doc.all.Text1.value = "123";

需要注意的2点
1:showModalDialog的第二个参数很关键
2:子窗口的dialogArguments对象来控制很重要

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值