js实现iframe跨页面调用函数的方法

2025-05-29 0 42

本文实例讲述了js实现iframe跨页面调用函数的方法。分享给大家供大家参考。具体实现方法如下:

在项目中难免会遇到这样一个问题就是页面引入了IFrame并且需要父页面调用子页面函数或者子页面需要调用父页面函数。比如说:现在有两个页面parent.html和child.html。其中parent.html中含有IFrame并且IFrame指向child.html。现在需要在parent.html/child.html中调用child.html/parent.html的一个js方法。

具体的代码实现如下:

parent.html父页面:

复制代码 代码如下:

<html>
<head>
<script type="text/javascript">
  function parent_click(){
    alert("来自父页面");
  }
</script>
</head>
<body>
  <input type="button" value="调用本页面函数" onclick="parent_click();" />
  <input type="button" value="调用子页面函数" onclick='window.frames["childPage"].child_click();' />
  <iframe id="childPage" name="childPage" src="inner.html" width="100%" frameborder="0"></iframe>
</body>
</html>

child.html子页面:

复制代码 代码如下:

<html>
<head>
<script type="text/javascript">
  function child_click(){
    alert("调用的子页面函数");
  }
</script>
</head>
<body>
  <input type="button" value="调用父页面函数" onclick='parent.window.parent_click();' />
  <input type="button" value="调用本页面函数" onclick="child_click();" />
</body>
</html>

希望本文所述对大家基于javascript的web程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 js实现iframe跨页面调用函数的方法 https://www.kuaiidc.com/110764.html

相关文章

发表评论
暂无评论