手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >Javascript 两个窗体之间传值实现代码
Javascript 两个窗体之间传值实现代码
摘要:如我们新建窗体FatherPage.htm:XML-Code:复制代码代码如下:functionOpenChildWindow(){wind...

如我们新建窗体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" />

然后在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" />

其实在打开子窗体的同时,我们也可以对子窗体的元素进行赋值,因为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" />

通过判断子窗体的引用是否为空,我们还可以控制使其只能打开一个子窗体:

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" />

光这样还不够,当关闭子窗体时还必须对父窗体的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" />

</body>

【Javascript 两个窗体之间传值实现代码】相关文章:

javascript实现youku的视频代码自适应宽度

在JavaScript中使用NaN值的方法

Javascript 不能释放内存.

在JavaScript中处理时间之setMinutes()方法的使用

javascript无刷新评论实现方法

在JavaScript应用中使用RequireJS来实现延迟加载

Javascript 字符串模板的简单实现

javascript检测两个数组是否相似

javascript自定义右键弹出菜单实现方法

javascript使用Promise对象实现异步编程

精品推荐
分类导航