javascript - 如何用JS操作iFrame里的Dom
迷茫
迷茫 2017-04-10 12:45:26
0
2
500

如题,父页面的JS如何拿到iframe里的dom,并且进行操作? 原生JS,不用JQ之类的 页面代码:

<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>​

 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

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

全員に返信(2)
伊谢尔伦

虽然是相对路径,还是出现跨域的错误。

最后找到原因了,文件之间的操作是按照跨域处理的,把文件放在web容器里,127.0.0.1 去访问就好了

いいねを押す +0
PHPzhong

额 哥们你说清楚一点嘛 !!!

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート