JavaScript獲取和控制CSS樣式對照表

   你們都知道,用document.getElementById(‘element’).style.xxx能夠獲取元素的樣式信息,但是它獲取的只是DOM元素style屬性裏的樣式規則,對於經過class屬性引用的外部樣式表,就拿不到咱們要的信息了。 web


   DOM標準裏有個全局方法getComputedStyle,能夠獲取到當前對象樣式規則信息,如:window.getComputedStyle(obj,null).paddingLeft,(obj表明獲取的節點名,第二個參數表明獲取僞元素信息,如‘:after’,不須要就null)就能獲取到對象的左內邊距。        
    可是事情還沒完,萬惡的IE不支持此方法,它有本身的一個實現方式,那就是currentStyle,不一樣於全局方法getComputedStyle,它是做爲DOM元素屬性存在的,如:obj.currentStyle.paddingLeft,在IE中就獲取到對象的左內邊距了。具體的兼容性寫法就留給大家本身研究了。 瀏覽器

而控制的方法就是統一的,如:obj.style.width='200px';
須要注意的就是CSS中用橫線鏈接的屬性如:line-height:10px;在js中就要寫成obj.style.lineHeight='10px';
下面就是具體的CSS和JS對照的屬性的寫法。 測試


而對於CSS3新添加的屬性須要寫瀏覽器前綴。
如-webkit-transform:rotate(20deg); js對應的寫法爲obj.style.webkitTransform='rotate(20deg)';
可是我發現最新的火狐瀏覽器不識別obj.style.mozTransform='rotate(20deg)';
而識別obj.style.MozTransform='rotate(20deg)';
因此我建議寫前綴仍是第一個字母大寫,如Ms、Moz、Webkit,至少這三大瀏覽器都識別這種寫法,其它的我沒測試過。 spa

CSS語法 (不區分大小寫)   JavaScript語法 (區分大小寫)
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
float                         floatStyle
margin                      margin
margin-bottom          marginBottom
margin-left                marginLeft
margin-right             marginRight
margin-top               marginTop
padding                    padding
padding-bottom       paddingBottom
padding-left             paddingLeft
padding-right           paddingRight
padding-top             paddingTop orm

顏色和背景標籤和屬性對照
CSS語法 (不區分大小寫)   JavaScript語法 (區分大小寫)
background                 background
background-attachment   backgroundAttachment
background-color        backgroundColor
background-image       backgroundImage
background-position   backgroundPosition
background-repeat      backgroundRepeat
color                           color
樣式標籤和屬性對照
CSS語法 (不區分大小寫)   JavaScript語法 (區分大小寫)
display                        display
list-style-type              listStyleType
list-style-image            listStyleImage
list-style-position         listStylePosition
list-style                       listStyle
white-space                 whiteSpace
文字樣式標籤和屬性對照
CSS語法 (不區分大小寫)   JavaScript語法 (區分大小寫)
font                             font
font-family                  fontFamily
font-size                     fontSize
font-style                    fontStyle
font-variant                 fontVariant
font-weight                 fontWeight
文本標籤和屬性對照
CSS語法 (不區分大小寫)   JavaScript語法 (區分大小寫)
letter-spacing              letterSpacing
line-break                    lineBreak
line-height                  lineHeight
text-align                    textAlign
text-decoration           textDecoration
text-indent                 textIndent
text-justify                  textJustify
text-transform             textTransform
vertical-align              verticalAlign 對象

其中有什麼錯誤的地方還望你們指出。 ip

相關文章
相關標籤/搜索