首頁  >  文章  >  web前端  >  js父頁面中使用子頁面的方法_javascript技巧

js父頁面中使用子頁面的方法_javascript技巧

WBOY
WBOY原創
2016-05-16 15:20:461317瀏覽

iframe是非常常用的一個html元素,如果在父頁面中使用子頁面的方法應該怎麼寫呢,下面就做一下簡單的介紹。
一、父頁碼

<html>
<head>
<meta charset=" gb2312">
<title>父页面</title>
<script type="text/javascript">
function parentFunction() 
{
 alert('function in parent');
}
function callChild() 
{
 child.window.childFunction();
 /*
  child 为iframe的name属性值,
  不能为id,因为在FireFox下id不能获取iframe对象
 */
}
</script>
</head>
<body>
 <iframe name="child" src="./child.html" ></iframe>
</body>
</html>

二、iframe中的程式碼

<html>
<head>
<meta charset="gb2312">
<title>iframe代码</title>
<script type="text/javascript">
function childFunction() 
{
 alert('function in child');
}
function callParent() 
{
 parent.parentFunction();
}
</script>
</head>
<body>
</body>
</html>

上面兩個程式碼可以在父頁和子頁面對對方的函數進行相互調用,比較簡單,不多介紹了。
希望本文所述對大家學習javascript程式設計有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn