jQuery 父iframe与子iframe 相互调用传值


来自:https://blog.csdn.net/wd4871/article/details/50517597 侵删

父页面中的iframe :如下

<iframe name="sunPage" id="sunPage" src="sun.html" width="300px" height="auto"></iframe>;

1.父iframe 调用子iframe的方法

    $("#sunPage")[0].contentWindow.sunMethod();
      contentWindow 对象可以获取子iframe的window对象,兼容所有浏览器.
      sunMethod() 这是子iframe中的方法名.

      如果iframe的id不知道,比如用jbox 的open方法打开一个ifram.可以借助jquery的find方法找到iframe节点。

      如:$(selector).find("iframe")[0].contentWondow.sunMethod();

2.父iframe 修改子iframe标签中的数据:如修改子iframe中的input的值.

      $('#sunPage').contents().find("#sunP").text("dsssssdd");

       jQuery contents() 方法: http://www.w3school.com.cn/jquery/traversing_contents.asp 介绍
       ID  #sunP 是子iframe中的input的id;

3.子iframe调用父iframe中的方法

    window.parent.daoYo("asdadasds");
      window.parent 直接调用window对象的parent
     daoYo("asdadasds"); 父页面的方法名,可传参数;

4,  子iframe 修改父iframe标签中的数据:如修改父iframe中的input的值.
        这个就不用说了.

       window.parent.$("#button3").text("ssssssssssss");


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM