如何用JS操作iFrame里的Dom
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用JS操作iFrame里的Dom相关的知识,希望对你有一定的参考价值。
<div id="myDiv">主页面</div><iframe id="myframe" width=420 height=330 ></iframe>
<script type="text/javascript">
var myframe=document.getElementById("myframe");
myframe.src=this.location.href;// 设置iframe的页面为当前页面
function invokemyframe()
myframe.contentDocument.getElementById("myDiv").innerText+="\\r\\n--修改了--"
setTimeout(invokemyframe,200);// 收尾延迟下,等待iframe的页面加载
</script>
上面的代码保存为一个html文件,在浏览器打开就可以查看了。
参考技术A <body><iframe id="container" name="container" src="frames/index.html"></iframe>
</body>
在chrome的console里:
document.getElementById("container")
Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.
Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.
Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.
<iframe id="container" name="container" src="frames/index.html">...</iframe>�6�7
document.getElementById("container").contentWindow.document
Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.
undefined
window.frames["container"].document
Blocked a frame with origin "null" from accessing a frame with origin "null". Protocols, domains, and ports must match.
undefined
以上是关于如何用JS操作iFrame里的Dom的主要内容,如果未能解决你的问题,请参考以下文章
ExtJs基础知识总结:DomIFrame和TreePanel