国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

Javascript 兩個窗體之間傳值實(shí)現(xiàn)代碼

2019-11-21 01:07:19
字體:
供稿:網(wǎng)友
如我們新建窗體FatherPage.htm:
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í)在打開子窗體的同時,我們也可以對子窗體的元素進(jìn)行賦值,因?yàn)閣indow.open函數(shù)同樣會返回一個子窗體的引用,因此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()" />

通過判斷子窗體的引用是否為空,我們還可以控制使其只能打開一個子窗體:
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)閉子窗體時還必須對父窗體的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>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 探索| 吉林省| 长治市| 行唐县| 凉山| 香港| 黄陵县| 健康| 金溪县| 道真| 手游| 秀山| 临城县| 廊坊市| 华亭县| 海南省| 界首市| 建德市| 泽库县| 绥中县| 东兴市| 永平县| 共和县| 托克逊县| 景德镇市| 望谟县| 山西省| 南城县| 婺源县| 绥中县| 尖扎县| 五莲县| 九龙坡区| 谢通门县| 玉林市| 阿图什市| 吐鲁番市| 德庆县| 渝北区| 柘城县| 霍邱县|