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程式設計有幫助。