2013-04-28 62 views

回答

5

,我认为这会工作:

var hasBackground = $("*").filter(function() { 
    return !!this.style.backgroundColor; 
}); 

...之后,hasBackground将一组元素在其上background-color风格一直直接集(例如,不通过样式规则) 。

但是我想申请“主题”的网站将换出CSS样式表,而不是单个元素的规则,这样的最佳方式:

<link rel="stylesheet" type="text/css" href="http://jsbin.com/evilit/1"> 
<link id="style" rel="stylesheet" type="text/css" href="http://jsbin.com/ireted/1"> 
<script> 
(function() { 

    setInterval(swapStyles, 1000); 

    function swapStyles() { 
    var style = $("#style"); 
    if (style[0].href.indexOf("ireted") === -1) { 
     style[0].href = "http://jsbin.com/ireted/1"; 
    } 
    else { 
     style[0].href = "http://jsbin.com/uhoqob/1"; 
    } 
    } 

})(); 
</script> 

Live Example | Source

其中http://jsbin.com/evilit/1是:

body { 
    font-size: 26pt; 
} 

http://jsbin.com/ireted/1是:

body { 
    background-color: yellow; 
} 

http://jsbin.com/uhoqob/1是:

body { 
    background-color: #eee; 
} 

注意,在本例中,有正在加载样式表。第一个定义字体大小,第二个(我们每秒换出)定义背景颜色。

+0

我的问题之一是,如果我将我的风格放入.css文件并应用它,整个网站就会崩溃。像没有任何显示,但坚实的backgroudn覆盖的颜色。我试过media =“Screen”等等,但它只是在.CSS文件中不起作用。这真的令人费解。 – WASasquatch 2013-04-28 21:44:14

+1

@WASasquatch:听起来像您可能会重写功能性而不是装饰性的样式。确保您要交换的样式表仅定义了可引用的内容。 – 2013-04-28 21:46:50

+0

http://jordan.rave5.com/tmpstuff/这是如果我用我的风格使用样式表会发生什么。背景图像中断和样式显示不正确。与http://jordan.rave5.com/tmpstuff/index_raw.html – WASasquatch 2013-04-28 21:52:15

0

将样式放入CSS文件时,忘了做绝对路径。