前言
日常開發中,經常用到在window中獲取某個iframe,或者在iframe中獲取當前window。這裏做個總結,記在心裏後,就不用每次都百度了。
iframe和iframe的關係
首先需要說明的是,w3c已經不推薦使用frame,而推薦使用iframe,iframe也就是inline frame(行內frame),顧名思義它具有css的行內框特性,正是因爲這一特性才引出來iframe 高度100%時,出現垂直滾動條
通過window獲取iframe
window.frames是個僞數組,可以通過window.frames[index]或window.frames[name]來獲取iframe
window.frames[index],索引是從左往右,從上往下的,從0開始,通常我們使用window.frames[name]來獲取frame
通過iframe獲取window、document
如果想獲取iframe裏的window或者document,可以使用
iframe.contentWindow、iframe.contentDocument
iframe.contentDocument=iframe.contentWindow.document,不過iframe.contentDocument在IE8及以下的版本不支持。
window獲取頂級窗口、父窗口
獲取頂級窗口:window.top
獲取父級窗口:window.parent
導航欄回退:history.back(); 注意回退的請求,會有緩存。
前進:history.forward()
history.back()和history.forward()僅僅是爲了方便分別代替history.go(-1)和history.go(1)
參考
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe
---------------------
作者:乾淨的句號
來源:CSDN
原文:https://blog.csdn.net/wangjun5159/article/details/78647615