如何用JS操作iFrame里的Dom

2025-04-04 10:19:38
推荐回答(2个)
回答(1):

主页面


  
 var myframe=document.getElementById("myframe");
 myframe.src=this.location.href;// 设置iframe的页面为当前页面
 function invokemyframe(){
    myframe.contentDocument.getElementById("myDiv").innerText+="\r\n--修改了--"
 }
 setTimeout(invokemyframe,200);// 收尾延迟下,等待iframe的页面加载

上面的代码保存为一个html文件,在浏览器打开就可以查看了。

回答(2):





在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.

�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

相关问答