javascript中使用css需要注意的地方小結(jié)
2.在使用“float”時(shí),因?yàn)椤癴loat”是javascript的一個(gè)保留字,所以就不能使用style.float,而改成了style.cssFloat(IE使用的是style.styleFloat);
3. 獲得元素的計(jì)算樣式:
在W3C DOM下可以:
var heading = document.getElementById("heading");
var computedStyle = document.defaultView.getComputedStyle(heading,null);
var computedFontFamily = computedStyle.fontFamily;//sans-serif
IE不支持使用DOM標(biāo)準(zhǔn)方法,可以:
var heading = document.getElementById("heading");
var computedFontFamily = heading.currentStyle.fontFamily;//sans-serif
綜合上述這些方法,可以創(chuàng)建一個(gè)跨瀏覽器函數(shù)來實(shí)現(xiàn)
function retrieveComputedStyle(element,styleProperty){
var computedStyle = null;
if(typeof element.currentStyle != "undefined"){
computedStyle = element.currentStyle;
}else{
computedStyle = document.defaultView.getComputedStyle(element,null);
}
return computedStyle[styleProperty];
}
對照表
CSS Properties To JavaScript Reference Conversion
CSS Property | JavaScript Reference |
---|---|
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 |
Usage
Internet Explorer
document.all.div_id.style.JS_property_reference = "new_CSS_property_value";
Older Netscape's (4.7 and earlier)
document.div_id.JS_property_reference = "new_CSS_property_value";
Netscape 6.0+ and Opera (and other Mozilla)
document.getElementById(div_id).style.JS_property_reference = "new_CSS_property_value";
Note the use of parentheses instead of square brackets in newer Mozilla's "getElementById()" reference.
相關(guān)文章
JS實(shí)現(xiàn)利用閉包判斷Dom元素和滾動(dòng)條的方向示例
這篇文章主要介紹了JS實(shí)現(xiàn)利用閉包判斷Dom元素和滾動(dòng)條的方向,涉及javascript閉包、事件響應(yīng)及頁面元素屬性動(dòng)態(tài)操作相關(guān)使用技巧,需要的朋友可以參考下2019-08-08微信小程序bindinput與bindsubmit的區(qū)別實(shí)例分析
這篇文章主要介紹了微信小程序bindinput與bindsubmit的區(qū)別,結(jié)合實(shí)例形式分析了微信小程序bindinput與bindsubmit的具體功能、用法及相關(guān)使用區(qū)別,需要的朋友可以參考下2019-04-04純js代碼制作的網(wǎng)頁時(shí)鐘特效【附實(shí)例】
下面小編就為大家?guī)硪黄僯s代碼制作的網(wǎng)頁時(shí)鐘特效【附實(shí)例】。小編覺得聽錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-03-03Swiper如何實(shí)現(xiàn)兩行四列輪播圖效果實(shí)例
大家應(yīng)該都知道,Swiper是純javascript打造的滑動(dòng)特效插件,面向手機(jī)、平板電腦等移動(dòng)終端,下面這篇文章主要給大家介紹了關(guān)于Swiper如何實(shí)現(xiàn)兩行四列輪播圖效果的相關(guān)資料,需要的朋友可以參考下2022-10-10利用js正則表達(dá)式驗(yàn)證手機(jī)號,email地址,郵政編碼
利用js正則表達(dá)式驗(yàn)證手機(jī)號,email地址,郵政編碼。需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01bootstrap表單按回車會(huì)自動(dòng)刷新頁面的解決辦法
這篇文章主要介紹了bootstrap表單按回車會(huì)自動(dòng)刷新頁面的問題及解決辦法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03詳解vue.js根據(jù)不同環(huán)境(正式、測試)打包到不同目錄
這篇文章主要介紹了詳解vue.js根據(jù)不同環(huán)境(正式、測試)打包到不同目錄,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07