我有一个简单的页面,其中包含一些iframe部分(以显示RSS链接)。如何将相同的CSS格式从首页应用于iframe中显示的页面?
如何将CSS应用于iframe?
As an alternative, you can use CSS-in-JS technology, like below lib:
https://github.com/cssobj/cssobj
It can inject JS object as CSS to iframe, dynamically
I think the easiest way is to add another div, in the same place as the iframe, then
make its z-index
bigger than the iframe container, so you can easly just style your own div. If you need to click on it, just use pointer-events:none
on your own div, so the iframe would be working in case you need to click on it ;)
I hope It will help someone ;)
You will not be able to style the contents of the iframe this way. My suggestion would be to use serverside scripting (PHP, ASP, or a Perl script) or find an online service that will convert a feed to JavaScript code. The only other way to do it would be if you can do a serverside include.
If you control the page in the iframe, as hangy said, the easiest approach is to create a shared CSS file with common styles, then just link to it from your html pages.
Otherwise it is unlikely you will be able to dynamically change the style of a page from an external page in your iframe. This is because browsers have tightened the security on cross frame dom scripting due to possible misuse for spoofing and other hacks.
This tutorial may provide you with more information on scripting iframes in general. About cross frame scripting explains the security restrictions from the IE perspective.
大多数浏览器普遍将iframe视为不同的HTML页面。如果要将相同的样式表应用于iframe的内容,只需从那里使用的页面引用它即可。
var $head = $("#eFormIFrame").contents().find("head");
$head.append($("<link/>", {
rel: "stylesheet",
href: url,
type: "text/css"
}));
Well, I have followed these steps:
iframe
iframe
to thediv
.This works in IE 6. Should work in other browsers, do check!