Html 使用一个 css 文件更改 iframe 中的正文背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10299515/
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
Change body background color in an iframe using one css file
提问by Denny Mueller
Using a iframe where I call a site from my webspace, Using one css file with body {background-color: #222}
.
使用 iframe 从我的网站空间调用站点,使用一个带有 .css 文件的 css 文件body {background-color: #222}
。
The iframe src also use this CSS file. The Problem is that the body in the iframe need another background-colour.
iframe src 也使用这个 CSS 文件。问题是 iframe 中的正文需要另一种背景颜色。
tried
试过了
iframe>body { background-color: #other }
Any idea or suggestions?
有什么想法或建议吗?
回答by Daan Twice
I assume the iframe takes up a smaller portion of your site. In that case I would advice you to simply use a div with the same size of the iframe is loaded, and giving this div the proper background-color.
我假设 iframe 占用您网站的一小部分。在这种情况下,我建议您简单地使用与加载的 iframe 大小相同的 div,并为该 div 提供适当的背景颜色。
OR: include a style property in the source of the iframe page:
或者:在 iframe 页面的源代码中包含一个样式属性:
<head>
<style>body {background-color:#COLOR}</style>
</head>
回答by walmik
This question is similar to How to apply css to iframe content?. Basically you can use jQuery to change the CSS properties in the iframe
这个问题类似于如何将 css 应用到 iframe 内容?. 基本上你可以使用 jQuery 来改变 iframe 中的 CSS 属性
$('#yourIframe').contents().find('body').css({
background-color: '#333333'
});
Edit:You may need to add that when the iframe loads:
编辑:您可能需要在 iframe 加载时添加:
$('#yourIframe').load(function(){
$(this).contents().find('body').css({
background-color: '#333333'
});
});
回答by Vito Schiavo
Remember that a Javascript can modify only properties of iframe with the same origin of the site, otherwise you'll get a Security Error.
请记住,Javascript 只能修改具有相同站点来源的 iframe 的属性,否则您将收到安全错误。
Protocols, domains and ports must match.
协议、域和端口必须匹配。
回答by Jeff
ive used this and it works for me. (and its sooooo easy!)
我用过这个,它对我有用。(而且非常简单!)
in your CSS file put:
在你的 CSS 文件中输入:
iframe { background-color:orange; }
iframe { 背景色:橙色;}
Just change the "orange" to whatever color you need. Thats it!
只需将“橙色”更改为您需要的任何颜色即可。就是这样!
Enjoy!
享受!
回答by Eric Frick
In the page that the iframe contains, link another stylesheet and then change all the CSS styles that you wish to.
在 iframe 包含的页面中,链接另一个样式表,然后更改您想要的所有 CSS 样式。