如何用JS操作iFrame里的Dom

2025-04-06 12:54:10
推荐回答(2个)
回答1:





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


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

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

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

回答2:

$("iframe").contents().find("#footer").remove();
$("iframe").contents().find("body").css("padding-top","0px").css("border","thin solid rgb(223, 223, 223)");

jquery 实现的