javascript父子页面(iframe)互相访问
javascript父子页面互相访问
如:main.html页面(父页面)有:
<body>
<div >
<input type="text" id="m1" name="m1"/>
<iframe name='frame1' id='frame1' src='sub.html' width='100%' height='100%' frameBorder='0'/>
</div>
</body>
子页面(sub.html)有:
<form id="form1">
<input type="text" id="input1" name="input1" value="111"/>
</form>
从父页面(main)访问子页面(sub)的form:
var frame = document.getElementById("frame1");
var inwin = frame.contentWindow;
var indoc = frame.contentDocument;
var form = indoc.getElementById("form1");
访问子页面的函数,变量,对象:
inwin.myFn(), inwin.myParam, inwin.myObj
从子页面(sub)访问父页面(main):
alert(parent == parent.window); //true
var outwin = parent;
var outdoc = parent.document;
var outinp = outdoc.getElementById("m1");
访问父页面的函数,变量,对象:
parent.myFn(), parent.myParam, parent.myObj
从frame访问document
var outdoc = frame.ownerDocument;//父页面的document
var indoc = frame.contentDocument;//子页面的document