Jquery取得iframe中元素的几种方法(转载)
Jquery取得iframe中元素的几种方法(转载)
iframe在复合文档中经常用到,利用jquery操作iframe可以大幅提高效率,这里收集一些基本操作
DOM方法:
父窗口操作IFRAME:
window.frames["iframeSon"].document
window.parent.document
$(window.frames["iframeSon"].document).find(":text");$(window.parent.document).find(":text");<iframe src="test.html" id="iframeSon" width="700″ height="300″ frameborder="0″ scrolling="auto"></iframe>
$(window.frames["iframe1"].document).find("input[@type='radio']").attr("checked","true");$(window.parent.document).find("input[@type='radio']").attr("checked","true");<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> </DIV> <DIV> </DIV> $(document.getElementById('iframeId').contentWindow.document.body).htm() $(document.getElementById('iframeId').contentWindow.document.body).htm() $("#testId", document.frames("iframename").document).html(); $("#testId", document.frames("iframename").document).html(); $(window.frames["iframeName"].document).find("#testId").html() $(window.frames["iframeName"].document).find("#testId").html()<iframe src="test.html" id="iframe1" width="700" height="300" frameborder="0" scrolling="auto"></iframe>
<iframe id="leftiframe"...</iframe><iframe id="mainiframe..</iframe>
$("#mainframe",parent.document.body).attr("src","http://www.radys.cn")<iframe id="mainifame"...></ifame>
<div id="someID">you want to get this content</div>
$("#mainiframe").contents().find("someID").html() html 或者 $("#mainiframe").contains().find("someID").text()值$(window.frames["iframe1"].document).find("#id") $("#mainframe",parent.document.body).contents().find("someID").html()或者 $("#mainframe",parent.document.body).contents().find("someID").val()window.frames["frameName"];window.frames.frameNamewindow.frames[index]
self.frames["frameName"]self.frames[0]frames[0]frameName
window.frames["frameName"];
window.frames["frameName"].frames["frameName2"];
<frameset rows="50%,50%"><frame src="1.html" name="frame1" /><frame src="2.html" name="frame2" /></frameset>
self.parent.frames["frame2"];
self.parent.frames["childName"].frames["targetFrameName"];
if(self==top){//dosomething}