使用一个css文件更改iframe中的正文背景颜色

Den*_*ler 5 html css iframe

使用iframe从我的网站空间调用网站,使用一个css文件body {background-color: #222}.

iframe src也使用这个CSS文件.问题是iframe中的主体需要另一种背景颜色.

试着

iframe>body { background-color: #other }
Run Code Online (Sandbox Code Playgroud)

有什么想法或建议吗?

Daa*_*ice 7

我假设iframe占据了您网站的一小部分.在这种情况下,我建议你只需使用与iframe相同大小的div加载,并为此div提供正确的背景颜色.

或者:在iframe页面的源代码中包含一个样式属性:

<head>
    <style>body {background-color:#COLOR}</style>
</head>
Run Code Online (Sandbox Code Playgroud)


wal*_*mik 5

此问题与如何将css应用于iframe内容类似.基本上你可以使用jQuery来改变iframe中的CSS属性

$('#yourIframe').contents().find('body').css({
    background-color: '#333333'
});
Run Code Online (Sandbox Code Playgroud)

编辑: 您可能需要在iframe加载时添加:

$('#yourIframe').load(function(){ 
    $(this).contents().find('body').css({
        background-color: '#333333'
    });
});
Run Code Online (Sandbox Code Playgroud)