<iframe src="./iframe.html" frameborder="0" scrolling="auto" name="iframe"></iframe>
window.frames.iframeName.onload = ...
,window.frames.iframeName.document
就是iframe的documentwindow.parent.document
就是父頁面的document<iframe src="./iframe.html" frameborder="0" scrolling="auto" name="iframe"></iframe>
複製代碼
屬性以下:javascript
window.frames
能夠獲取頁面全部的iframe元素。 獲取iframe的的window:css
window.frames.iframeName
,主要這裏的iframeName是iframe的name屬性值。這時候就能夠用window的一系列屬性了。document.getElementById("frameid").contentWindow
window.frames.iframeName.onload = funcetion(){...}
<!-- index.html -->
<iframe src ="/iframe.html" id="test" name="test" scrolling="yes">
<p>Your browser does not support iframes.</p>
</iframe>
<script> // iframe.html裏面的window var iwindow_alias = document.getElementById("testid"); var iwindow = window.frames.test; // 獲取iframe的元素就須要在iframe加載後 iwindow.onload = function() { // iframe.html裏面的document var idoc = iwindow.document; // iframe.html裏面的body var ibody = idoc.body; // iframe.html裏面的元素 var iele = iwindow.document.querySelector("a"); console.log(idoc, ibody, iele); }; </script>
複製代碼
在iframe頁面裏,經過訪問window.parent
,引用它的父框架的window。html
<!-- iframe.html -->
<button>點擊</button>
<script> // index.html裏面的window var pwindow = window.parent // index.html裏面的document var pdoc = pwindow.document // index.html裏面的body var pbody = pdoc.body // index.html裏面的a元素之類的 var pele = pdoc.querySelector('a') </script>
複製代碼