父元素與子iframe相互獲取變量和元素對(duì)象的具體實(shí)現(xiàn)
更新時(shí)間:2013年10月15日 17:07:01 作者:
父元素與子iframe相互獲取變量和元素對(duì)象的方法有很多,本文提供了一些不錯(cuò)的示例另收集網(wǎng)上的一些,可以參考下
父中:
<input id="username"type="text" />
<scripttype="text/javascript">
var count = 1;
</script>
-------------------------------------------------------------
子iframe中:
<scripttype="text/javascript">
alert(window.parent.count); //獲取父中的js變量
alert($("#username", window.parent.document));//獲取父中jQuery對(duì)象
alert(window.parent.document.getElementByIdx_x('username'));//獲取父中DOM對(duì)象
</script>
--------------------------------------------------------------------------------------------------
子iframe中(id="iframeId"name="iframeName"):
<input id="username"type="text" />
<scripttype="text/javascript">
varcount = 1;
</script>
-------------------------------------------------------------
父中:
<scripttype="text/javascript">
alert(document.iframeName.count);//獲取子iframe中的js變量
alert($(window.frames["iframeName"].document).contents().find("#username"));//獲取子iframe中jQuery對(duì)象
alert($("#username",document.frames("iframeName").document));//獲取子iframe中jQuery對(duì)象
alert(window.document.getElementById('iframeId').contentWindow.document.getElementByIdx_x('username'));//獲取子iframe中DOM對(duì)象
</script>
收集網(wǎng)上的一些示例:
用jQuery在IFRAME里取得父窗口的某個(gè)元素的值
只好用DOM方法與jquery方法結(jié)合的方式實(shí)現(xiàn)了
1. 在父窗口中操作 選中IFRAME中的所有單選鈕
$(window.frames["iframe1"].document).find("input:radio").attr("checked","true");
2. 在IFRAME中操作 選中父窗口中的所有單選鈕
$(window.parent.document).find("input:radio").attr("checked","true");
父窗口想獲得IFrame中的Iframe,就再加一個(gè)frames子級(jí)就行了,如:
$(window.frames["iframe1"].frames["iframe2"].document).find("input:radio").attr("checked","true");
3.在子窗口中調(diào)用父窗口中的另一個(gè)子窗口的方法(FRAME):
parent.frames["Main"].Fun();
注意:建議使用[],這樣比較兼容多個(gè)瀏覽器,()火狐/搜狗/谷歌不兼容。
復(fù)制代碼 代碼如下:
<input id="username"type="text" />
<scripttype="text/javascript">
var count = 1;
</script>
-------------------------------------------------------------
子iframe中:
復(fù)制代碼 代碼如下:
<scripttype="text/javascript">
alert(window.parent.count); //獲取父中的js變量
alert($("#username", window.parent.document));//獲取父中jQuery對(duì)象
alert(window.parent.document.getElementByIdx_x('username'));//獲取父中DOM對(duì)象
</script>
--------------------------------------------------------------------------------------------------
子iframe中(id="iframeId"name="iframeName"):
復(fù)制代碼 代碼如下:
<input id="username"type="text" />
<scripttype="text/javascript">
varcount = 1;
</script>
-------------------------------------------------------------
父中:
復(fù)制代碼 代碼如下:
<scripttype="text/javascript">
alert(document.iframeName.count);//獲取子iframe中的js變量
alert($(window.frames["iframeName"].document).contents().find("#username"));//獲取子iframe中jQuery對(duì)象
alert($("#username",document.frames("iframeName").document));//獲取子iframe中jQuery對(duì)象
alert(window.document.getElementById('iframeId').contentWindow.document.getElementByIdx_x('username'));//獲取子iframe中DOM對(duì)象
</script>
收集網(wǎng)上的一些示例:
用jQuery在IFRAME里取得父窗口的某個(gè)元素的值
只好用DOM方法與jquery方法結(jié)合的方式實(shí)現(xiàn)了
1. 在父窗口中操作 選中IFRAME中的所有單選鈕
$(window.frames["iframe1"].document).find("input:radio").attr("checked","true");
2. 在IFRAME中操作 選中父窗口中的所有單選鈕
$(window.parent.document).find("input:radio").attr("checked","true");
父窗口想獲得IFrame中的Iframe,就再加一個(gè)frames子級(jí)就行了,如:
$(window.frames["iframe1"].frames["iframe2"].document).find("input:radio").attr("checked","true");
3.在子窗口中調(diào)用父窗口中的另一個(gè)子窗口的方法(FRAME):
parent.frames["Main"].Fun();
注意:建議使用[],這樣比較兼容多個(gè)瀏覽器,()火狐/搜狗/谷歌不兼容。
相關(guān)文章
document.createElement()用法及注意事項(xiàng)(ff下不兼容)
今天處理了一個(gè)日期選擇器的ie和ff的兼容問(wèn)題,本來(lái)這種情況就很難找錯(cuò)誤,找了好久才把錯(cuò)誤定位到j(luò)s中創(chuàng)建元素的方法document.createElement(),這個(gè)方法在ie下支持這樣創(chuàng)建元素2013-03-03JavaScript定義變量和變量?jī)?yōu)先級(jí)問(wèn)題探討
這篇文章主要介紹了JavaScript定義變量和變量?jī)?yōu)先級(jí)的問(wèn)題探討,變量的定義還有這么講究嗎,不錯(cuò),看完本文相信你會(huì)有一定的收獲,需要的朋友可以參考下2014-10-10JavaScript四種調(diào)用模式和this示例介紹
JavaScript調(diào)用時(shí)除了聲明時(shí)定義的形參外,每個(gè)函數(shù)接受兩個(gè)附加參數(shù):this 和arguments,下面為大家介紹下JavaScript四種調(diào)用模式和this2014-01-01在js里怎么實(shí)現(xiàn)Xcode里的callFuncN方法(詳解)
下面小編就為大家?guī)?lái)一篇在js里怎么實(shí)現(xiàn)Xcode里的callFuncN方法(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11js輸入框郵箱自動(dòng)提示功能代碼實(shí)現(xiàn)
一個(gè)輸入框當(dāng)我輸入任何字的時(shí)候自動(dòng)下拉相應(yīng)的郵箱提示,在輸入框輸入11的時(shí)候下拉框有所有11的郵箱 輸入其他的時(shí)候 有其他文案對(duì)應(yīng)的郵箱,下面實(shí)現(xiàn)這個(gè)自動(dòng)提示功能2013-12-12JavaScript實(shí)現(xiàn)的浮動(dòng)層框架用法實(shí)例分析
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的浮動(dòng)層框架用法,以實(shí)例形式分析了JavaScript實(shí)現(xiàn)可關(guān)閉的半透明浮動(dòng)層相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10