我有一个简单的页面,其中包含一些iframe部分(用于显示RSS链接)。我如何将主页面的相同CSS格式应用于在iframe中显示的页面?
我有一个简单的页面,其中包含一些iframe部分(用于显示RSS链接)。我如何将主页面的相同CSS格式应用于在iframe中显示的页面?
这里,域内有两个事物:
因此,您希望按照以下方式为这两个部分设计样式:
1. iFrame 部分的样式
可以使用 CSS 样式来设计具有相应 id
或 class
名称的样式。您也可以在父样式表中直接为其设置样式。
<style>
#my_iFrame{
height: 300px;
width: 100%;
position:absolute;
top:0;
left:0;
border: 1px black solid;
}
</style>
<iframe name='iframe1' id="my_iFrame" src="#" cellspacing="0"></iframe>
2. 为iFrame内加载的页面添加样式
可以使用Javascript从父页面加载这些样式。
var cssFile = document.createElement("link")
cssFile.rel = "stylesheet";
cssFile.type = "text/css";
cssFile.href = "iFramePage.css";
//to Load in the Body Part
frames['my_iFrame'].document.body.appendChild(cssFile);
//to Load in the Head Part
frames['my_iFrame'].document.head.appendChild(cssFile);
在这里,您也可以使用此方法编辑iFrame内页面的头部部分
var $iFrameHead = $("#my_iFrame").contents().find("head");
$iFrameHead.append(
$("<link/>",{
rel: "stylesheet",
href: urlPath,
type: "text/css" }
));
z-index
设置得比iframe容器更大,这样你就可以轻松地为自己的div设置样式。如果需要单击它,则只需在自己的div上使用pointer-events:none
,这样在需要单击它时iframe仍将起作用。希望对某些人有所帮助;)这是我在生产环境中的做法。需要注意的是,如果iframe属于其他网站,它将会触发CORS错误并且无法工作。
var $iframe = document.querySelector(`iframe`);
var doc = $iframe.contentDocument;
var style = doc.createElement("style");
style.textContent = `*{display:none!important;}`;
doc.head.append(style);
load
事件附加到iframe:var $iframe = document.querySelector(`iframe`);
$iframe.addEventListener("load", function() {
var doc = $iframe.contentDocument;
var style = doc.createElement("style");
style.textContent = `*{display:none!important;}`;
doc.head.append(style);
});
var link1 = document.createElement('link');
link1.type = 'text/css';
link1.rel = 'stylesheet';
link1.href = "../../assets/css/normalize.css";
window.frames['richTextField'].document.body.appendChild(link1);
richTextField
是什么? - Kirankumar Dafda作为替代方案,您可以使用 CSS-in-JS 技术,比如下面的库:
它可以动态地将JS对象注入到iframe中作为CSS。有一个神奇的脚本,它可以用iframe版本替换节点。 CodePen 演示
使用示例:
// Single node
var component = document.querySelector('.component');
var iframe = iframify(component);
// Collection of nodes
var components = document.querySelectorAll('.component');
var iframes = Array.prototype.map.call(components, function (component) {
return iframify(component, {});
});
// With options
var component = document.querySelector('.component');
var iframe = iframify(component, {
headExtra: '<style>.component { color: red; }</style>',
metaViewport: '<meta name="viewport" content="width=device-width">'
});
这只是一个概念,但不要在没有进行安全检查和过滤的情况下实现它!否则脚本可能会黑掉您的网站!
回答:如果您控制目标站点,可以设置接收器脚本如下:
1)使用style
参数设置iframe链接,例如:
http://your_site.com/target.php?color=red
(最后一个短语是由urlencode
函数编码的a{color:red}
)。
2)将接收页面target.php
设置为以下内容:
<head>
..........
$col = FILTER_VAR(SANITIZE_STRING, $_GET['color']);
<style>.xyz{color: <?php echo (in_array( $col, ['red','yellow','green'])? $col : "black") ;?> } </style>
..........
好的,我已经按照以下步骤进行了操作:
div
元素来容纳 iframe
。iframe
添加到 div
中。divClass { width: 500px; height: 500px; }
divClass iframe { width: 100%; height: 100%; }
在IE 6中可以工作。应该在其他浏览器中也能工作,请检查!