解析Jquery取得iframe中元素的几种方法


iframe在复合文档中经常用到,利用jquery操作iframe可以大幅提高效率,这里收集一些基本操作,需要的朋友可以参考下。

DOM方法:
父窗口操作IFRAME:window.frames["iframeSon"].document //假如iframe的id为iframeSon 

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框架的:

 1 <HTML xmlns="http://www.w3.org/1999/xhtml">      
 2  <HEAD>      
 3      <MCE:SCRIPT mce_src="js/jquery-1.2.6.js" src="../js/jquery-1.2.6.js" type="text/ecmascript">MCE:SCRIPT>      
 4      <MCE:SCRIPT type="text/javascript">MCE:SCRIPT>      
16  <DIV>      
17  <INPUT id=t1>      
18  <IFRAME id=iframe1 src="child.htm" mce_src="child.htm">IFRAME>      
19  <IFRAME height=100 src="child.htm" width=300 mce_src="child.htm">IFRAME>      
20  DIV>      
21  <DIV>      
22  DIV>      

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

代码如下:

$(document.getElementById('iframeId').contentWindow.document.body).htm()  

显示iframe中body元素的内容。代码如下:

$("#testId", document.frames("iframename").document).html();

根据iframename取得其中ID为"testId"元素。代码如下:

$(window.frames["iframeName"].document).find("#testId").html()

作用同上

收集网上的一些示例:
jQueryIFRAME里取得父窗口的某个元素的值

只好用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.jb51.net")

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