色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

Javascript 兩個窗體之間傳值實現代碼

如我們新建窗體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>

JavaScript技術Javascript 兩個窗體之間傳值實現代碼,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 奉新县| 巴中市| 镇远县| 卢氏县| 万载县| 金乡县| 蓬溪县| 大兴区| 左权县| 方正县| 阳西县| 越西县| 灯塔市| 柳林县| 绥德县| 黄冈市| 乌兰察布市| 策勒县| 延吉市| 汶川县| 秦皇岛市| 阳山县| 双江| 苍南县| 凤庆县| 蒙自县| 乌什县| 尖扎县| 宜章县| 资阳市| 天门市| 正安县| 买车| 应用必备| 聂拉木县| 临泽县| 鞍山市| 开化县| 尼木县| 阜新市| 东宁县|