A-A+

jquery操作iframe中的元素和js函数

2016年01月13日 前端设计 暂无评论 阅读 4 views 次

iframe调用界面我们可以使用上级函数来进行操作了,这个功能小编常会使用到,今天一起来看一个例子,具体细节如下所示。

1、jquery操作iframe中的元素(2种方式)

  1. var str = $(window.frames["iframe"].document).find("#ljiong").html();  
  2. var stk = $("#iframe").contents().find("#ljiong").html();  

2、操作父界面中的元素(header:为某个元素的id)

  1. $('#header', parent.document).text();  

3、js调用iframe中的js函数(2种)

  1. window.frames["ljiong"].window.testIframe2("ljiong");  
  2. document.getElementById("ljiong").contentWindow.testIframe2("ljiong");  

4、jquery调用iframe的js函数(带参数的会有返回值)

  1. $("#ljiong")[0].contentWindow.testIframe2("ljiong");  

5. 调用父页面js函数,直接用:

parent.myFunction();

注意事项:

1、要和所包含的iframe在同一个域名(因为不能跨域)

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

jquery方法:

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

在IFRAME中操作 选中父窗口中的所有输入框:$(window.parent.document).find(":text");

iframe框架的HTML:

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

  1. <iframe src="test.html" id="iframe1″ width="700″ height="300″ frameborder="0″ scrolling="auto"></iframe>  
  2.    
  3. <HTML xmlns="http://www.w3.org/1999/xhtml">        
  4.  <HEAD>        
  5.      <MCE:SCRIPT mce_src="js/jquery-1.2.6.js" src="../js/jquery-1.2.6.js" type="text/ecmascript"></MCE:SCRIPT>        
  6.      <MCE:SCRIPT type="text/javascript"><!--      
  7.          $(function(){        
  8.              $("#t1").hover(function(){alert('');});        
  9.              //$("iframe").contents().find("body").append("I'm in an iframe!");         
  10.              //$(window.frames["iframe1"].document).find("input[@type='text']").attr("size","30px");        
  11.              //$("#iframe1").contents().find("#d1").css('color','red');        
  12.              //$(window.frames["iframe1"].document).find("input[@name='t1']").css({background:"#369"});        
  13.              //$("#iframe1").src("test.html");        
  14.          });       
  15.  // --></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()

作用同上

网上整理到的方法

内容里有两个ifame

  1. <iframe id="leftiframe"...</iframe>   
  2. <iframe id="mainiframe..</iframe>   

leftiframe中jQuery改变mainiframe的src代码:

$("#mainframe",parent.document.body).attr("src","https://www.xiariboke.net ")

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

  1. <iframe id="mainifame"...></ifame>   

ifame包含一个someID

  1. <div id="someID">you want to get this content</div>   

得到someID的内容

$("#mainiframe").contents().find("someID").html() html 或者 $("#mainiframe").contains().find("someID").text()值

如上面所示

leftiframe中的jQuery操作mainiframe的内容someID的内容

$("#mainframe",parent.document.body).contents().find("someID").html()或者 $("#mainframe",parent.document.body).contents().find("someID").val()

在父窗口中操作 选中IFRAME中的所有单选钮

$(window.frames["iframe1"].document).find("input[ at type='radio']").attr("checked","true");

在IFRAME中操作 选中父窗口中的所有单选钮

$(window.parent.document).find("input[ at type='radio']").attr("checked","true");

iframe框架的:

  1. <iframe src="test.html" id="iframe1" width="700" height="300" frameborder="0" scrolling="auto"></iframe>  

IE7中测试通过

在父页面访问Iframe子窗体的txtAddress控件

window.frames["ifrMapCompanyDetails"].document.all("txtAddress").value = '地址' ;

在Iframe子窗体1访问父页面的TextBox1控件 , 子窗体1把值赋给子窗体2的某个控件

string strValue = "从子窗体传递给父页面的值" ;

下面是在Page_Load事件里面调用的,当然可以写在javascript脚本里面

this.Response.Write("parent.document.all('TextBox1').value = '" + strValue + "';");

this.Response.Write("if( parent.document.all('TextBox2').value = '0')parent.document.all('TextBox1').value = '44';");

子窗体访问父窗体中的全局变量:

parent.xxx;

在Iframe子窗体1访问子窗体2的txtAddress控件 子窗体1把值赋给子窗体2的某个控件

window.parent.frames["ifrMapCompanyDetails"].document.all("txtAddress").value = '地址' ;

父窗体提交两个Iframe子窗体

window.frames["ifrMapCompanyDetails"].Form1.submit();

window.frames["ifrMapProductInfoDetails"].Form1.submit();

Iframe子窗体 调用父页面的javascript事件

window.parent.XXX()

//父页面调用当前页面中IFRAME子页面中的脚本childEvent

function invokechildEvent()

{ var frm = document.frames["ifrChild1"].childEvent(); }

或者调用当前页面中第一个IFRAME中的脚本childEvent

{ var frm = document.frames[0]; frm.childEvent(); }

//子页面调用父窗体的某个按钮的按钮事件

window.parent.Form1.btnParent.click()

父页面调用子窗体的某个按钮的按钮事件

window.frames['ifrChild1'].document.all.item("btnChild3").click();

//jquery 部分:

在父窗口中操作 选中IFRAME中的所有单选钮

$(window.frames["iframe1"].document).find("input[ at type='radio']").attr("checked","true");

在IFRAME中操作 选中父窗口中的所有单选钮

$(window.parent.document).find("input[ at type='radio']").attr("checked","true");

标签:

给我留言