Jquery取得iframe中元素的几种方法(转载)



iframe在复合文档中经常用到,利用jquery操作iframe可以大幅提高效率,这里收集一些基本操作

DOM方法:
父窗口操作IFRAME:window.frames["iframeSon"].document
IFRAME操作父窗口: window.parent.document

jquery方法:
在父窗口中操作 选中IFRAME中的所有输入框: $(window.frames["iframeSon"].document).find(":text");
在IFRAME中操作 选中父窗口中的所有输入框:$(window.parent.document).find(":text");

iframe框架的HTML:

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");
iframe框架的:

01  
02 <HTML xmlns="http://www.w3.org/1999/xhtml">    
03 <HEAD>    
04          
05     <MCE:SCRIPT mce_src="js/jquery-1.2.6.js" src="../js/jquery-1.2.6.js" type="text/ecmascript">MCE:SCRIPT>    
06     <MCE:SCRIPT type="text/javascript">MCE:SCRIPT>    
18      
19      
20 <DIV>    
21 <INPUT id=t1>    
22 <IFRAME id=iframe1 src="child.htm" mce_src="child.htm">IFRAME>    
23 <IFRAME height=100 src="child.htm" width=300 mce_src="child.htm">IFRAME>    
24 DIV>    
25 <DIV>    
26 DIV>    
27     

 

收集利用Jquery取得iframe中元素的几种方法 :

 

Js代码
  1. $(document.getElementById('iframeId').contentWindow.document.body).htm()  
$(document.getElementById('iframeId').contentWindow.document.body).htm()

 

显示iframe中body元素的内容。

 

Js代码
  1. $("#testId", document.frames("iframename").document).html();  
$("#testId", document.frames("iframename").document).html();

根据iframename取得其中ID为"testId"元素

 

Js代码
  1. $(window.frames["iframeName"].document).find("#testId").html()  
$(window.frames["iframeName"].document).find("#testId").html()

作用同上

 

收集网上的一些示例:

用jQuery在IFRAME里取得父窗口的某个元素的值

只好用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");

iframe框架的:

IE7中测试通过

 

 

使用jquery操作iframe

1、 内容里有两个ifame

leftiframe中jQuery改变mainiframe的src代码: 
$("#mainframe",parent.document.body).attr("src","http://www.radys.cn")

2、 如果内容里面有一个ID为mainiframe的ifame