父頁面: 子頁面: ...
父頁面:
<!DOCTYPE html> <html> <head> <title>parents</title> <meta charset="UTF-8"> </head> <body>
<div id="default">div內容</div> <iframe src="child.html" id="child"></iframe>
<script language="javascript" type="text/javascript">
function sayhello() {
alert('Hello World!');
}
var value = 'value';
child.contentWindow.test(); </script> </body> </html>
子頁面:
<!DOCTYPE html> <html> <head> <title>child</title> <meta charset="UTF-8"> <script language="javascript" type="text/javascript" src="js/jquery.min.js"></script> <script language="javascript" type="text/javascript"> $(function() { //在iframe子頁面中查找父頁面元素 alert($('#default', window.parent.document).html()); //在iframe中調用父頁面中定義的變數 alert(parent.value); //在iframe中調用父頁面中定義的方法 parent.sayhello(); });
function test() { alert('我是子頁面!'); }
</script> </head> <body> <div>iframe子頁面內容</div> </body> </html>