源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

解决同一页面中两个iframe互相调用jquery,js函数的方法

  • 时间:2020-05-03 15:00 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:解决同一页面中两个iframe互相调用jquery,js函数的方法
这一个月又没更新博客,唉,懒癌又犯了,今天解决了一个问题,关于两个iframe互相调用jquery函数方法 a.html中有两个iframe,如下:
<iframe width="100%" height="100%" name="left" scrolling="auto" frameborder="0" src="b.html" id="left"></iframe>
<iframe width="100%" height="100%" name="main1" scrolling="auto" frameborder="0" src="c.html" id="main1" ></iframe>
b.html中有一个treeview,称为左菜单iframe c.html为b.html中treeview点击后显示的数据,就称为右页面iframe。 既然两个页面之间都有关系,存在在a.html中,那这两个页面就能互相调用Js方法。 [b]ps:如果两个页面之间没有一点关系,那就让他们产生关系方能互相调用,比如说两个页面都引用一个js文件,那就能互相调用了。[/b] 比如说 c.html中有一个button
<input type="button" id="btn" onclick="btnAlert(i)"/>
<input type="hidden" id="hidVal" name="hidVal" />
function btnAlert(index) {
 $(window.parent.$("#left"))[0].contentWindow.GetIndex(index);//调用b.html中的方法
}
b.html中
 function GetIndex(index) { 
   $(window.parent.$("#main1")).contents().find("#hidVal").val(index);//找到c.html中的hidVal给他赋值
 }
[b]最后总结下:[/b] 如果是c.html的iframe是在b.html中那么引用下面的方法 [b]1,子iframe内调用父类函数方法:[/b] [code]window.parent.func();[/code] [b]2,子Iframe中获取父界面的元素:[/b] [code]$("#xx", window.parent.document);[/code] 这个xx就是父界面中要获取的元素的ID。 [b]3,jquery 调用子iframe页面中js的方法:[/b]  iframefunction()是子页面的方法 [code]$(window.parent.document).contents().find("#iframename")[0].contentWindow.iframefunction(); [/code] 以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持编程素材网!
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部