国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 城口县| 石嘴山市| 哈尔滨市| 杭锦旗| 丹凤县| 四会市| 西昌市| 临湘市| 南和县| 台湾省| 永顺县| 个旧市| 永川市| 崇州市| 进贤县| 梓潼县| 石泉县| 安宁市| 阳谷县| 桑日县| 宣威市| 杭锦旗| 吉水县| 定日县| 屏边| 宁明县| 富平县| 布尔津县| 四会市| 九龙坡区| 海口市| 固始县| 成武县| 镇平县| 固阳县| 思茅市| 南阳市| 昌都县| 万源市| 额敏县| 曲周县|