Home > Web Front-end > JS Tutorial > Summary of several methods of jquery operating iframe_jquery

Summary of several methods of jquery operating iframe_jquery

WBOY
Release: 2016-05-16 17:09:17
Original
1250 people have browsed it

iframe is often used in compound documents. Using jquery to operate iframe can greatly improve efficiency. Here are some basic operations

DOM method:
Parent window operation IFRAME: window.frames["iframeSon"].document
IFRAME operation parent window: window.parent.document

jquery method:
Select all input boxes in the IFRAME in the parent window: $(window.frames["iframeSon"].document).find(":text");
Select all input boxes in the parent window in IFRAME: $(window.parent.document).find(":text");

iframe HTML:

1. Select all radio buttons in the IFRAME in the parent window
$(window.frames["iframe1"].document).find("input[@type=' radio']").attr("checked","true");

2. Select all radio buttons in the parent window in the IFRAME
$(window.parent.document).find("input[@type='radio']") .attr("checked","true");

iframe frame:

Copy code The code is as follows:

                                                                  ="../js/jquery-1.2.6.js" type="text/ecmascript">  
 < !--                                                                                                                                                                        ").contents().find("body").append("I'm in an iframe!");                                                                 [@type='text']").attr("size","30px");  
        //$("#iframe1").contents().find("#d1").css(' color','red');
//$(window.frames["iframe1"].document).find("input[@name='t1']").css({background:"#369 "});                                                                                                                                                   ;


; ;
🎜>



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

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

显示iframe中body元素的内容。

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

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

$(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.jb51.net")

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