js父頁面中使用子頁面的方法
更新時間:2016年01月09日 19:02:25 作者:一落葉而知秋
這篇文章主要向大家介紹了js父頁面中使用子頁面的方法,即js父頁面使用iframe中的函數(shù),感興趣的朋友可以參考一下
iframe是非常常用的一個html元素,如果在父頁面中使用子頁面的方法應(yīng)該怎么寫呢,下面就做一下簡單的介紹。
一、父頁面代碼
<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>
上面兩個代碼可以在父頁面和子頁面對對方的函數(shù)進行相互調(diào)用,比較簡單,不多介紹了。
希望本文所述對大家學(xué)習(xí)javascript程序設(shè)計有所幫助。
相關(guān)文章
JavaScript實現(xiàn)仿網(wǎng)易通行證表單驗證
這篇文章主要介紹了JavaScript實現(xiàn)仿網(wǎng)易通行證表單驗證,十分的實用,有需要的小伙伴可以參考下。2015-05-05