导航首页 » 技术教程 » 解决同一页面中两个iframe互相调用jquery,js函数的方法
解决同一页面中两个iframe互相调用jquery,js函数的方法 129 2024-02-09   

这一个月又没更新博客,唉,懒癌又犯了,今天解决了一个问题,关于两个iframe互相调用jquery函数方法

a.html中有两个iframe,如下:

<iframe width="100%" height="100%" name="left" scrolling="auto" frameborder="0" src="http://www.gimoo.net/t/1810/b.html" id="left"></iframe>
<iframe width="100%" height="100%" name="main1" scrolling="auto" frameborder="0" src="http://www.gimoo.net/t/1810/c.html" id="main1" ></iframe>

b.html中有一个treeview,称为左菜单iframe

c.html为b.html中treeview点击后显示的数据,就称为右页面iframe。

既然两个页面之间都有关系,存在在a.html中,那这两个页面就能互相调用Js方法。

ps:如果两个页面之间没有一点关系,那就让他们产生关系方能互相调用,比如说两个页面都引用一个js文件,那就能互相调用了。

比如说

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给他赋值
 }

最后总结下:

如果是c.html的iframe是在b.html中那么引用下面的方法

1,子iframe内调用父类函数方法:

window.parent.func();

2,子Iframe中获取父界面的元素:

$("#xx", window.parent.document);

这个xx就是父界面中要获取的元素的ID。

3,jquery 调用子iframe页面中js的方法:

iframefunction()是子页面的方法

$(window.parent.document).contents().find("#iframename")[0].contentWindow.iframefunction();

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持绿夏网!


!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103