JavaScript 設置/讀取 CSS 的值
通常直接在元素設置 style 時會遇到的問題、因為 style 和 JavaScript 在 CSS 上面的寫法會有些不同。
div.div1{background-color:#9acd32;
font-size:3em; line-height:3em;
padding:0px 20px;}
Html (HyperText Markup Language)
<div class="div1" style="color:#fff;">div1</div>
div1
<div class="div1" style="color:#fff;">div1</div>
var div = document.querySelector("div.div1");
div.style.backgroundColor = "#006400";
div.setAttribute("style", "background-color:gold;");
div.removeAttribute("style");
註 div1 的 color:#fff 會因 setAttribute 而被取代。顏色 #006400 轉換成 RGB 之差異。
window.getComputedStyle() 讀取 CSS 的值(只讀)
window.getComputedStyle() 方法返回該對象所應用的樣式表,並解析這些值可能包含的任何基本計算後報告元素的所有 CSS 屬性的值。簡單地使用 CSS 屬性名稱進行索引來訪問。
getComputedStyle 的返回值是 resolved values 通常跟 CSS2.1 中的 computed values 是相同的值。
https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle
div.div1{background-color:#9acd32;
font-size:3em; line-height:3em;
padding:0px 20px;}
<div class="div1" style="color:#fff;">div1</div>
div2
var div = document.querySelector("div.div1");
var cssCollection = window.getComputedStyle(div, null);
var result = cssCollection.getPropertyValue("background-color");
-
註 padding 及 padding-top 會因瀏覽器不同、例如 null 或 0px 20px。
所以在複合的參數 border-top 要指明完整屬性 border-top-width , border-top-color 才能正確返回結果,意思就是交代清楚方向。
CSS Property Style 的屬性表示 | JavaScript Reference Style 的屬性表示 |
---|---|
background | background |
background-attachment | backgroundAttachment |
background-color | backgroundColor |
background-image | backgroundImage |
background-position | backgroundPosition |
background-repeat | backgroundRepeat |
border | border |
border-bottom | borderBottom |
border-bottom-color | borderBottomColor |
border-bottom-style | borderBottomStyle |
border-bottom-width | borderBottomWidth |
border-color | borderColor |
border-left | borderLeft |
border-left-color | borderLeftColor |
border-left-style | borderLeftStyle |
border-left-width | borderLeftWidth |
border-right | borderRight |
border-right-color | borderRightColor |
border-right-style | borderRightStyle |
border-right-width | borderRightWidth |
border-style | borderStyle |
border-top | borderTop |
border-top-color | borderTopColor |
border-top-style | borderTopStyle |
border-top-width | borderTopWidth |
border-width | borderWidth |
clear | clear |
clip | clip |
color | color |
cursor | cursor |
display | display |
filter | filter |
font | font |
font-family | fontFamily |
font-size | fontSize |
font-variant | fontVariant |
font-weight | fontWeight |
height | height |
left | left |
letter-spacing | letterSpacing |
line-height | lineHeight |
list-style | listStyle |
list-style-image | listStyleImage |
list-style-position | listStylePosition |
list-style-type | listStyleType |
margin | margin |
margin-bottom | marginBottom |
margin-left | marginLeft |
margin-right | marginRight |
margin-top | marginTop |
overflow | overflow |
padding | padding |
padding-bottom | paddingBottom |
padding-left | paddingLeft |
padding-right | paddingRight |
padding-top | paddingTop |
page-break-after | pageBreakAfter |
page-break-before | pageBreakBefore |
position | position |
float | styleFloat |
text-align | textAlign |
text-decoration | textDecoration |
text-decoration: blink | textDecorationBlink |
text-decoration: line-through | textDecorationLineThrough |
text-decoration: none | textDecorationNone |
text-decoration: overline | textDecorationOverline |
text-decoration: underline | textDecorationUnderline |
text-indent | textIndent |
text-transform | textTransform |
top | top |
vertical-align | verticalAlign |
visibility | visibility |
width | width |
z-index | zIndex |
列出所有樣式表的集合 Style Collection
可以比較瀏覽器不同之下的差異。
var div = document.querySelector("div.div1");
var getStyleCollection = window.getComputedStyle(div, null);
for (var i = 0; i < getStyleCollection.length; i++) {
var cssName = getStyleCollection[i];
var cssValue = window.getComputedStyle(div, null).getPropertyValue(cssName);
$("#Results").append(cssName + " = " + cssValue);
}
-