首页 > 语言 > JavaScript > 正文

jquery 操作iframe的几种方法总结

2024-05-06 14:31:55
字体:
来源:转载
供稿:网友

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:<iframe src="test.html" id="iframeSon" width="700″ height="300″ frameborder="0″ scrolling="auto"></iframe>

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框架的:
<iframe src="test.html" id="iframe1″ width="700″ height="300″ frameborder="0″ scrolling="auto"></iframe>
代码如下:
 <HTML xmlns="http://www.w3.org/1999/xhtml">    
 <HEAD>    

     <MCE:SCRIPT mce_src="js/jquery-1.2.6.js" src="../js/jquery-1.2.6.js" type="text/ecmascript"></MCE:SCRIPT>    
     <MCE:SCRIPT type="text/javascript"><!--   

         $(function(){    
             $("#t1").hover(function(){alert('');});    
             //$("iframe").contents().find("body").append("I'm in an iframe!");     
             //$(window.frames["iframe1"].document).find("input[@type='text']").attr("size","30px");    
             //$("#iframe1").contents().find("#d1").css('color','red');    
             //$(window.frames["iframe1"].document).find("input[@name='t1']").css({background:"#369"});    
             //$("#iframe1").src("test.html");    
         });    

 // --></MCE:SCRIPT>    

 <DIV>    
 <INPUT id=t1>    
 <IFRAME id=iframe1 src="child.htm" mce_src="child.htm"></IFRAME>    
 <IFRAME height=100 src="child.htm" width=300 mce_src="child.htm"></IFRAME>    

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选