iframe父子页面间的相互调用
父页面parent.html
HTML:
<div>我是父页面</div>
<iframe id="parentId" src="child.html"></iframe>
JavaScript:
function parentWay(){
alert("我是父页面的方法!");
}
//父页面调用子页面的方法
function parentCallChild(){
document.getElementById("parentId").contentWindow.childWay();
}
//父页面调用子页面的元素
function parentCallChildVar(){
$("#parentId").contents().find("#clickId").click();
}
子页面child.html
HTML:
<div>我是子页面</div>
<button id="clickId" onclick="clickWay()"></button>
JavaScript:
function childWay(){
alert("我是子页面的方法!");
}
function clickWay(){
alert("我是子页面点击方法!");
}
//子页面调用父页面的方法
function childCallParent(){
window.parent.parentWay();
}