在實際的編程中,有時我們需要在父表單訪問子表單的方法或對子表單進行訪問、操作,又或者我們在子表單內對父表單進行訪問或調用方法,實現很簡單。見以下樣本:
" id="Code_Closed_Image_105656" style="display: none;">" id="Code_Open_Image_105656">Code
父表單(webform1.aspx):
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>父表單</title>
<script type="text/javascript">
var Sys=function(){};
Sys.sayHello=function(){
alert('Hello parent/opner page!!');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<iframe id="ifm"></iframe>
<input type="button" onclick="document.getElementById('ifm').src='webform2.aspx'; //window.open('webform2.aspx')" value="Open Child" />
<input type="button" onclick="window.frames['ifm'].Sys.sayHello()" value="Invoke Child Domain" />
</form>
</body>
</html>
子表單(webform2.aspx):
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>子表單</title>
<script type="text/javascript">
var Sys=function(){};
Sys.sayHello=function(){
alert('Hello child page!!');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input type="button" value="Invoke Parent/Opner Domain" onclick="window.parent.Sys.sayHello();" />
</form>
</body>
</html>