這篇文章主要介紹了js實(shí)現(xiàn)iframe跨頁(yè)面調(diào)用函數(shù)的方法,實(shí)例展示了iframe中父頁(yè)面調(diào)用子頁(yè)面和子頁(yè)面調(diào)用父頁(yè)面的實(shí)現(xiàn)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了js實(shí)現(xiàn)iframe跨頁(yè)面調(diào)用函數(shù)的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
在項(xiàng)目中難免會(huì)遇到這樣一個(gè)問(wèn)題就是頁(yè)面引入了IFrame并且需要父頁(yè)面調(diào)用子頁(yè)面函數(shù)或者子頁(yè)面需要調(diào)用父頁(yè)面函數(shù)。比如說(shuō):現(xiàn)在有兩個(gè)頁(yè)面parent.html和child.html。其中parent.html中含有IFrame并且IFrame指向child.html。現(xiàn)在需要在parent.html/child.html中調(diào)用child.html/parent.html的一個(gè)js方法。
具體的代碼實(shí)現(xiàn)如下:
parent.html父頁(yè)面:
復(fù)制代碼代碼如下:
<html>
<head>
<script type="text/javascript">
function parent_click(){
alert("來(lái)自父頁(yè)面");
}
</script>
</head>
<body>
<input type="button" value="調(diào)用本頁(yè)面函數(shù)" onclick="parent_click();" />
<input type="button" value="調(diào)用子頁(yè)面函數(shù)" onclick='window.frames["childPage"].child_click();' />
<iframe id="childPage" name="childPage" src="inner.html" width="100%" frameborder="0"></iframe>
</body>
</html>
child.html子頁(yè)面:
復(fù)制代碼代碼如下:
<html>
<head>
<script type="text/javascript">
function child_click(){
alert("調(diào)用的子頁(yè)面函數(shù)");
}
</script>
</head>
<body>
<input type="button" value="調(diào)用父頁(yè)面函數(shù)" onclick='parent.window.parent_click();' />
<input type="button" value="調(diào)用本頁(yè)面函數(shù)" onclick="child_click();" />
</body>
</html>
希望本文所述對(duì)大家基于javascript的web程序設(shè)計(jì)有所幫助。