现在的位置: 首页 > 综合 > 正文

js 模式子窗口和父窗口传值

2014年01月31日 ⁄ 综合 ⁄ 共 1661字 ⁄ 字号 评论关闭

父窗口.html:

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>父窗口(通过opener传值)</title>

<script>

 

function openModalDialog(){

var father=window;//建立一个名为"father"的父窗口(本窗口)引用。也可以是: father=this;

var oo=new Array("子窗体的标题:模式子窗体和父窗体的传值",father);//将要传递到子窗体的内容都用array封装.

var strr=window.showModalDialog("模式子窗口.html",oo);//阻塞函数,等子模式窗口关闭的时候才能传回值

  var showContent=document.getElementById("showContent");

showContent.innerHTML=strr;//显示子窗口返回的字符串,这是第二种方式,只不过要等子模式窗口关闭才能收到值。

}

</script>

</head>

 

<body>

<!--打开模式子窗口window.showModalDialog(url,obj);-->

<input type="button" value="打开一个普通子窗口" onclick="openModalDialog();"/>

<font color="red" id="wenzi"></font>

<div id="showContent" style="font-size:30px;">

</div>

</body>

</html>


模式子窗口.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
 var father;//定义一个变量存放父窗体传过来的父窗体引用。
 function getValue(){//获取父窗口传递的内容
var values=window.dialogArguments;
document.title=values[0];//把第一个参数作为窗口标题。
father=values[1];//把第二个参数赋值给father
}
 function reString(){ 
father.document.getElementById("showContent").innerHTML="从子模式窗口返回的字符串:我是子窗口";
//也可以这样: 
window.returnValue="从子模式窗口返回的字符串:我也是子窗口";//然后在父窗口中用一个变量接收,然后显示
}
</script>
</head>
<body onload="getValue();"><!--通过获取本窗口的opener来获取父窗口对象的引用,来操作-->
<input type="text" id="reValue" />
<input type="button" value="将文本框的文本返回到父窗口" onclick="reString();"/>
<hr />
</body>
</html>

抱歉!评论已关闭.