Javascript 兩個(gè)窗體之間傳值實(shí)現(xiàn)代碼
更新時(shí)間:2009年09月25日 14:46:45 作者:
眾所周知window.open() 函數(shù)可以用來打開一個(gè)新窗口,那么如何在子窗體中向父窗體傳值呢,其實(shí)通過window.opener即可獲取父窗體的引用。
如我們新建窗體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()" />
其實(shí)在打開子窗體的同時(shí),我們也可以對(duì)子窗體的元素進(jìn)行賦值,因?yàn)閣indow.open函數(shù)同樣會(huì)返回一個(gè)子窗體的引用,因此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()" />
通過判斷子窗體的引用是否為空,我們還可以控制使其只能打開一個(gè)子窗體:
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()" />
光這樣還不夠,當(dāng)關(guān)閉子窗體時(shí)還必須對(duì)父窗體的child變量進(jìn)行清空,否則打開子窗體后再關(guān)閉就無法再重新打開了:
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:
復(fù)制代碼 代碼如下:
<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:
復(fù)制代碼 代碼如下:
<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()" />
其實(shí)在打開子窗體的同時(shí),我們也可以對(duì)子窗體的元素進(jìn)行賦值,因?yàn)閣indow.open函數(shù)同樣會(huì)返回一個(gè)子窗體的引用,因此FatherPage.htm可以修改為:
XML-Code:
復(fù)制代碼 代碼如下:
<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()" />
通過判斷子窗體的引用是否為空,我們還可以控制使其只能打開一個(gè)子窗體:
XML-Code:
復(fù)制代碼 代碼如下:
<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()" />
光這樣還不夠,當(dāng)關(guān)閉子窗體時(shí)還必須對(duì)父窗體的child變量進(jìn)行清空,否則打開子窗體后再關(guān)閉就無法再重新打開了:
XML-Code:
復(fù)制代碼 代碼如下:
<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用DIV模擬彈出窗口_窗體滾動(dòng)跟隨
- Javascript showModalDialog兩個(gè)窗體之間傳值
- javascript 通過封裝div方式彈出div窗體
- JavaScript 彈出窗體點(diǎn)擊按鈕返回選擇數(shù)據(jù)的實(shí)現(xiàn)
- javascript 子窗體父窗體相互傳值方法
- 父子窗體間傳遞JSON格式的數(shù)據(jù)的代碼
- js關(guān)閉子窗體刷新父窗體實(shí)現(xiàn)方法
- 利用javaScript實(shí)現(xiàn)點(diǎn)擊輸入框彈出窗體選擇信息
- 鼠標(biāo)拖拽移動(dòng)子窗體的JS實(shí)現(xiàn)
- Ext JS動(dòng)態(tài)加載JavaScript創(chuàng)建窗體的方法
相關(guān)文章
使用 JavaScript如何獲取當(dāng)月的第一天和最后一天
這篇文章主要介紹了使用 JavaScript如何獲取當(dāng)月的第一天和最后一天,通過本文學(xué)習(xí)了如何使用 JavaScript 中的Date.getFullYear()和?Date.getMonth()方法獲得某個(gè)特定月份的第一天和最后一天,需要的朋友可以參考下2023-05-05JavaScript轉(zhuǎn)換農(nóng)歷類實(shí)現(xiàn)及調(diào)用方法
農(nóng)歷是日常生活中不可或缺的一部分,它與人類的生活息息相關(guān),從某種程度上說,它一直伴隨著我們,今天的任務(wù)是JavaScript轉(zhuǎn)換農(nóng)歷類的實(shí)現(xiàn),感興趣的你可以千萬不要錯(cuò)過,希望本文對(duì)你有所幫助2013-01-01jQuery?事件綁定及取消?bind?live?delegate?on?one區(qū)別解析
這篇文章主要介紹了jquery?事件綁定及取消?bind?live?delegate?on?one區(qū)別解析,本文給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11javascript復(fù)制粘貼與clipboardData的使用
window.clipboardData可以實(shí)現(xiàn)復(fù)制與粘貼的操作,下面有個(gè)小示例,想學(xué)習(xí)的朋友可以參考下2014-10-10