如何控制iframe中主体元素的背景图像和颜色?注意,嵌入的body元素有一个类,iframe是属于我的站点的一个页面。
我需要这个的原因是,我的网站有一个黑色背景分配给主体,然后一个白色背景分配给包含文本的div。所见即所得编辑器在编辑时使用iframe来嵌入内容,但它不包括div,因此文本很难阅读。
iframe的主体在编辑器中有一个在其他任何地方都不使用的类,所以我假设这个放在那里是为了解决这样的问题。然而,当我将样式应用到类时。它们不会覆盖应用于Body的样式。奇怪的是,样式确实出现在Firebug中,所以我不知道发生了什么!
谢谢
更新-我已经尝试了@mikeq的解决方案,添加一个样式的类,是身体的类。这在添加到主页的样式表时不起作用,但在添加到Firebug时起作用。我假设这是因为Firebug应用于页面上的所有元素,而CSS没有应用于iframes中。这是否意味着添加css后窗口加载与JavaScript将工作?
覆盖另一个域iframe CSS
通过使用SimpleSam5的部分答案,我通过一些Tawk的聊天iframe实现了这一点(他们的定制界面很好,但我需要进一步的定制)。
在移动设备上显示的这个iframe中,我需要隐藏默认图标,并放置一个背景图像。我做了以下事情:
Tawk_API.onLoad = function() {
// without a specific API, you may try a similar load function
// perhaps with a setTimeout to ensure the iframe's content is fully loaded
$('#mtawkchat-minified-iframe-element').
contents().find("head").append(
$("<style type='text/css'>"+
"#tawkchat-status-text-container {"+
"background: url(https://example.net/img/my_mobile_bg.png) no-repeat center center blue;"+
"background-size: 100%;"+
"} "+
"#tawkchat-status-icon {display:none} </style>")
);
};
我不拥有任何Tawk的域名,这对我来说是有用的,因此你可以这样做,即使它不是来自同一个父域(尽管Jeremy Becker对Sam的回答发表了评论)。
覆盖另一个域iframe CSS
通过使用SimpleSam5的部分答案,我通过一些Tawk的聊天iframe实现了这一点(他们的定制界面很好,但我需要进一步的定制)。
在移动设备上显示的这个iframe中,我需要隐藏默认图标,并放置一个背景图像。我做了以下事情:
Tawk_API.onLoad = function() {
// without a specific API, you may try a similar load function
// perhaps with a setTimeout to ensure the iframe's content is fully loaded
$('#mtawkchat-minified-iframe-element').
contents().find("head").append(
$("<style type='text/css'>"+
"#tawkchat-status-text-container {"+
"background: url(https://example.net/img/my_mobile_bg.png) no-repeat center center blue;"+
"background-size: 100%;"+
"} "+
"#tawkchat-status-icon {display:none} </style>")
);
};
我不拥有任何Tawk的域名,这对我来说是有用的,因此你可以这样做,即使它不是来自同一个父域(尽管Jeremy Becker对Sam的回答发表了评论)。