Javascript 如何跨域更改iframe内容的样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4724904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:46:52  来源:igfitidea点击:

How to change style of iframe content cross-domain?

javascripthtmlcssiframe

提问by ace

I want to make background color black and text color white for the content inside iframe from its default of normal white background and black text. The iframe src attribute points to different domain to which I have no access or cannot place any file or stylesheets in that domain. So given these conditions is it possible to make just these style changes in the iframe content and if so then how?

我想让 iframe 内的内容的背景颜色为黑色,文本颜色为白色,默认为正常的白色背景和黑色文本。iframe src 属性指向我无权访问或无法在该域中放置任何文件或样式表的不同域。因此,鉴于这些条件,是否可以在 iframe 内容中仅进行这些样式更改,如果可以,那么如何更改?

回答by Sean Patrick Floyd

The only possibility would be to load the iframe content through a proxy of yours and modify the HTML content. You can not access iframes from another domain via JavaScript.

唯一的可能性是通过您的代理加载 iframe 内容并修改 HTML 内容。您无法通过 JavaScript 从另一个域访问 iframe。

回答by Andy E

It isn't possible. The whole point of the Same Origin Policyis that you can't access or manipulate content from another domain.

这是不可能的。同源策略的全部意义在于您无法访问或操纵来自另一个域的内容。

回答by scmehetio

This one's been here a while, but hopefully this will help someone....

这个已经在这里一段时间了,但希望这会帮助某人......

The closest I can see is using a filter on the iframe

我能看到的最接近的是在 iframe 上使用过滤器

Xray: makes it black and white then inverts it.

X 射线:使其变为黑白,然后将其反转。

 <iframe style="filter:xray" src=".....

Invert: inverts all the colors rather than just swapping black & white.

反转:反转所有颜色,而不仅仅是交换黑白。

 <iframe style="filter:invert" src=".....

Both only work in IE. I thought these only worked on images till a few minutes ago...

两者都只适用于 IE。直到几分钟前,我还以为这些仅适用于图像...

eg. http://www.ssi-developer.net/css/visual-filters.shtml

例如。 http://www.ssi-developer.net/css/visual-filters.shtml