jquery 父窗口 子窗口 相關操作

<1> js或者jQuery訪問頁面中的框架iframe.
注意:框架內的頁面是不能跨域的! 假設有兩個頁面,在相同域下.

 

假設:父窗口  index.html ,有 id 爲 subifrm 的 iframe

 

1. 在index.html執行JS直接訪問子窗口中某元素 :

document.getElementById('subifrm').contentWindow.document.getElementById('test').style.color='red'

 

2. 利用jquery 來訪問子窗口

$("#subifrm").contents().find("#test").css('color','red');

====================================================================

====================================================================

 

<2> 用DOM方法與jquery方法結合的方式實現互動操作

1.在父窗口中操作 選中IFRAME中的所有單選鈕

$(window.frames["iframe1"].document).find("input[@type='radio']").attr("checked","true");

 


2.在IFRAME中操作 選中父窗口中的所有單選鈕

$(window.parent.document).find("input[@type='radio']").attr("checked","true");

====================================================================

====================================================================

 

<3> 使用jquery操作iframe

1 頁面裏有兩個ifame

<iframe id="leftiframe"></iframe>


<iframe id="mainiframe></iframe>

 

 

leftiframe中jQuery改變mainiframe的src代碼:

$("#mainframe",parent.document.body).attr("src","http://www.baidu.com")
 

 

2、 如果內容裏面有一個ID爲mainiframe的ifame

<iframe id="mainifame"></ifame> 
 

ifame包含一個someID

<div id="someID">you want to get this content</div> 

 

得到someID的內容

$("#mainiframe").contents().find("someID").html();或者$("#mainiframe").contains().find("someID").text();
 

2 、如上面所示
   leftiframe中的jQuery操作mainiframe的內容someID的內容

$("#mainframe",parent.document.body).contents().find("someID").html();或者 $("#mainframe",parent.document.body).contents().find("someID").val();
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章