Javascript 两个窗体之间传值实现代码
作者:bea
如我们新建窗体FatherPage.htm: XML-Code: 代码如下: <script type="text/javascript"> function OpenChildWindow() { window.open('ChildPage.htm'); } </script> <input type="text" id="txtInput" /> <input type="button" value="Ope
如我们新建窗体FatherPage.htm:
XML-Code:
代码如下:
<script type="text/javascript">
function OpenChildWindow()
{
window.open('ChildPage.htm');
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="OpenChild" onclick="OpenChildWindow()" />
然后在ChildPage.htm中即可通过window.opener来访问父窗体中的元素:
XML-Code:
代码如下:
<script type="text/javascript">
function SetValue()
{
window.opener.document.getElementById('txtInput').value
=document.getElementById('txtInput').value;
window.close();
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="SetFather" onclick="SetValue()" />
其实在打开子窗体的同时,我们也可以对子窗体的元素进行赋值,因为window.open函数同样会返回一个子窗体的引用,因此FatherPage.htm可以修改为:
XML-Code:
代码如下:
<script type="text/javascript">
function OpenChildWindow()
{
var child = window.open('ChildPage.htm');
child.document.getElementById('txtInput').value
=document.getElementById('txtInput').value;
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="OpenChild" onclick="OpenChildWindow()" />
通过判断子窗体的引用是否为空,我们还可以控制使其只能打开一个子窗体:
XML-Code:
代码如下:
<script type="text/javascript">
var child
function OpenChildWindow()
{
if(!child)
child = window.open('ChildPage.htm');
child.document.getElementById('txtInput').value
=document.getElementById('txtInput').value;
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="OpenChild" onclick="OpenChildWindow()" />
光这样还不够,当关闭子窗体时还必须对父窗体的child变量进行清空,否则打开子窗体后再关闭就无法再重新打开了:
XML-Code:
代码如下:
<body onunload="Unload()">
<script type="text/javascript">
function SetValue()
{
window.opener.document.getElementById('txtInput').value
=document.getElementById('txtInput').value;
window.close();
}
function Unload()
{
window.opener.child=null;
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="SetFather" onclick="SetValue()" />
</body>
有用 | 无用
XML-Code:
代码如下:
<script type="text/javascript">
function OpenChildWindow()
{
window.open('ChildPage.htm');
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="OpenChild" onclick="OpenChildWindow()" />
然后在ChildPage.htm中即可通过window.opener来访问父窗体中的元素:
XML-Code:
代码如下:
<script type="text/javascript">
function SetValue()
{
window.opener.document.getElementById('txtInput').value
=document.getElementById('txtInput').value;
window.close();
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="SetFather" onclick="SetValue()" />
其实在打开子窗体的同时,我们也可以对子窗体的元素进行赋值,因为window.open函数同样会返回一个子窗体的引用,因此FatherPage.htm可以修改为:
XML-Code:
代码如下:
<script type="text/javascript">
function OpenChildWindow()
{
var child = window.open('ChildPage.htm');
child.document.getElementById('txtInput').value
=document.getElementById('txtInput').value;
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="OpenChild" onclick="OpenChildWindow()" />
通过判断子窗体的引用是否为空,我们还可以控制使其只能打开一个子窗体:
XML-Code:
代码如下:
<script type="text/javascript">
var child
function OpenChildWindow()
{
if(!child)
child = window.open('ChildPage.htm');
child.document.getElementById('txtInput').value
=document.getElementById('txtInput').value;
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="OpenChild" onclick="OpenChildWindow()" />
光这样还不够,当关闭子窗体时还必须对父窗体的child变量进行清空,否则打开子窗体后再关闭就无法再重新打开了:
XML-Code:
代码如下:
<body onunload="Unload()">
<script type="text/javascript">
function SetValue()
{
window.opener.document.getElementById('txtInput').value
=document.getElementById('txtInput').value;
window.close();
}
function Unload()
{
window.opener.child=null;
}
</script>
<input type="text" id="txtInput" />
<input type="button" value="SetFather" onclick="SetValue()" />
</body>
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- JavaScript 自动在表格前面增加序号
- PPK 谈 JavaScript 的 this 关键字 [翻译]
- 一个JS小玩意 几个属性相加不能超过一个特定值.
- javascript String split方法误操作
- IE FF OPERA都可用的弹出层实现代码
- javascript 表单验证常见正则
- javascript 页面划词搜索JS
- jquery 模式对话框终极版实现代码
- 使用JS判断是否数字和小数点组合的数字的两中方法比较(isNaN和逐判断)
- javascript的onchange事件与jQuery的change()方法比较
- 支持ie与FireFox的剪切板操作代码
- javaScript 简单验证代码(用户名,密码,邮箱)
- JS 连锁泡泡 v1.1
- 一个特殊的排序需求的javascript实现代码
- javascript parseInt 大改造
- 网页自动跳转代码收集
- JavaScript中Object和Function的关系小结
- 用Javascript 获取页面元素的位置的代码
- Javascript showModalDialog两个窗体之间传值