本文實體講述了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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/146318.html
標籤:JavaScript
