在项目中难免会遇到这样一个问题就是页面引入了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 ></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>

 

http://blog.csdn.net/qingfeilee/article/details/7280171

 

相关文章:

  • 2021-10-11
  • 2021-10-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-27
猜你喜欢
  • 2021-04-14
  • 2022-12-23
  • 2021-06-09
  • 2021-08-27
  • 2021-07-28
  • 2021-08-27
  • 2022-12-23
相关资源
相似解决方案