子頁面 通過 JS 操作 父頁面的內容

 

 比如 main.html 面的相關信息如下

<body >
<H1 id="h1Title"> H1內容</H1>
<iframe id="leftFrame" name="leftFrame" src="page/includes/left.html"></iframe>
<iframe id="centerFrame" name="centerFrame" width="537px" height="1000px"  src="page/includes/center.html"></iframe>		
</body>


如果 left.html  中想改變 main.html 中H1 的內容 則可以通過如下方式來改變

window.parent.document.getElementById("h1Title").innerHTML = "aabbccdd";

 

如果在center.html  要想執行left.html 中的JS函數 testLeft(),則方法如下:

top.window.frames["leftFrame"].testLeft() 即可。

 

注意事項: iframe 必須有 name 屬性 否則 在火狐瀏覽其中 top.window.frames["leftFrame"].*是不起作用的! 也就是找不大到對應“名稱”的iframe

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章