小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

js子窗口和父窗口的數(shù)據(jù)傳遞和交互的應(yīng)用

 cicy000 2012-11-20

js子窗口和父窗口的數(shù)據(jù)傳遞和交互的應(yīng)用

子窗口和父窗口交互的內(nèi)容,是把子窗口的信息傳遞給父窗口,并且關(guān)閉自己等等,或者是父窗口把自己的信息傳遞給子窗口等等。

1。父窗口傳遞信息給子窗口

看代碼實(shí)例:
<script language=javascript>

function outPut()
{
//獲取父窗口的文本信息賦值給text
var text = document.abc.text.value;
//打開(kāi)子窗口,并且把操作句柄賦值給win變量,以下所有操作都是針對(duì)win對(duì)象的
var win = window.open(”",”mywin”, “menubar=no,width=400,height=100,resizeable=yes”);
//輸出基本信息
win.document.writeln(”<title>輸出結(jié)果</title>”);
win.document.writeln(”你的信息是:<p>”);
//輸出從父窗口獲取的信息
win.document.writeln(text);
win.document.close();
win.focus();
}
</script>

<form name=abc method=post>
<input type=text name=text size=50>
//調(diào)用上面的函數(shù)
<input type=button value=提交 onClick=”outPut()”>

</form>

2。子窗口傳遞參數(shù)給父窗口

我們對(duì)上面的代碼進(jìn)行改造:

<script language=javascript>

function outPut()
{
var text = document.abc.text.value;
var win = window.open(”",”mywin”, “menubar=no,width=400,height=100,resizeable=yes”);
win.document.writeln(”<title>輸出結(jié)果</title>”);
win.document.writeln(”你的信息是:<p>”);
win.document.writeln(text);
win.document.writeln(”<input type=text name=child value=子窗口信息>”);

//對(duì)子窗口本身操作,使用self對(duì)象,對(duì)父窗口操作使用opener對(duì)象,這是關(guān)鍵
//把子窗口中表單的值回傳給父窗口,取代父窗口表單以前的值,然后關(guān)閉子窗口
win.document.writeln(”<input type=button value=關(guān)閉自己 onClick= window.opener.abc.text.value=self.child.value;self.close() >”);
//可以控制關(guān)閉父窗口
win.document.writeln(”<input type=button value=關(guān)閉父窗口 onClick= window.opener.opener=null;window.opener.close() >”);
//刷新父窗口
win.document.writeln(”<input type=button value=刷新父窗口 onClick= window.opener.location.reload() >”);

win.document.close();
win.focus();
}
</script>

<form name=abc method=post>
<input type=text name=text size=50>
<input type=button value=提交 onClick=”outPut()”>

</form>

3。不是同頁(yè)面的子窗口和父窗口交互

假設(shè)我們涉及到外部程序,比如php、asp等等,那么我們處理的可能是兩個(gè)頁(yè)面,比如,上傳功能,我們就是需要打開(kāi)一個(gè)新頁(yè)面,然后再把新頁(yè)面中的值傳遞給父頁(yè)面。

局部代碼實(shí)例:

<input type=”input” value=”" name=”input_tag” id = “input_tag” onKeyUp=”clearPreTagStyle()” size=”40″>
<input type=”hidden” value=”0″ name=”tagid” id=”tagid”>
<input type=”button” value=”標(biāo)簽” onclick=”popUpWindow( tag.php?tag= +escape(document.tryst_form.input_tag.value))”>

以上是父窗口的部分代碼,里面的popUpWindow是封裝好的window.open函數(shù),所以理解面面的tag.php是另外一個(gè)頁(yè)面就可以,我們需要把當(dāng)前表單中的值提交給tag.php頁(yè)面去處理。


tag.php部分代碼:

查詢標(biāo)簽結(jié)果:
<a href=”#” name=”tag_1″>生活</a><a href=”#” onclick=”opener.document.tryst_form.input_tag.value = document.tag_1.innerHTML”>加入該標(biāo)簽</a>

<a href=”#” name=”tag_2″>生活秀</a><a href=”#” onclick=”opener.document.tryst_form.input_tag.value = document.tag_2.innerHTML”>加入該標(biāo)簽</a>

這個(gè)就是我們的子窗口,g:w7垠件的專e 育,網(wǎng)E我們要把tag_1和tag_2返回到子窗口中,雖然他們不是同一個(gè)頁(yè)面。這里有個(gè)知識(shí)點(diǎn),就是我們?nèi)绾潍@取連接中的值,我們使用 innerHTML屬性:document.tag_2.innerHTML 這個(gè)就是我們獲取了tag_2的值“生活秀”,我們也能使用其他方法獲取,比如:document.all.tag_2.innerHTML,

或者this.innerHTML就是指本身的鏈接的值。

訪問(wèn)父窗口也是使用opener對(duì)象來(lái)處理:opener.document.tryst_form.input_tag.value,

就能夠改變父窗口的值。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多