|
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技術(shù):Javascript 兩個(gè)窗體之間傳值實(shí)現(xiàn)代碼,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。