<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>
复制代码