2012-10-25 76 views
2

我有一个div获得一个风格如何使用控制台

<div class="blue>; 

类蓝色是:

.blue { 
      background-color: blue; 
    } 

现在我知道我可以使用设置div的背景色控制台:

 $0.style.backgroundColor = "#ffcc00" 

但是,如果我想要使用控制台获取该元素的背景颜色值?

回答

0

你可以这样做:

var blue = document.getElementsByClassName('blue')[0]; 

blue.style.getPropertyCSSValue('background-color'); 

,或者你做:

blue.style.getPropertyValue('background-color'); 
+0

我希望你注意到控制台执行JavaScript,没有特殊的命令语法 – pfried

0

这是可能的,你可能要计算样式:

var style = getComputedStyle(document.body, null); // Gets the style for a passed element and optional pseudo selecter (eg. :hover) 
console.log(style.backgroundColor); 

需要注意的是计算出的风格是很重要呈现结果。如果您对同一个元素有多个规则,则只会显示已应用的规则。