剛剛看到一篇好的文章介紹js呼叫css屬性,( ^_^ )不錯嘛!免的自己忘記,總結一下
1.對於沒有中劃線的css屬性一般直接使用style.屬性名即可。
如:obj.style.margin,obj.style.width,obj.style.left,obj.style.position等。
2.對於含有中劃線的css屬性,將每個中劃線去掉並將每個中劃線後的第一個字元換成大寫即可。
如:obj.style.marginTop,obj.style.borderLeftWidth,obj.style.zIndex,obj.style.fontFamily等。
因為float是Javascript的保留字,那要怎麼在js中書寫樣式表中的float呢?
我們不能直接使用obj.style.float來使用,這樣操作是無效的。
其正確的使用方法為:IE:obj.style.styleFloat,其他瀏覽器Mozilla(gecko),ff等用styleFloat:obj.style.cssFloat。
給個例子讓大家好理解:
複製代碼代碼如下:
<divonclick="alert(this.style.float);
this.style.float='left';
alert(this.style.float);">測試1</div>
<divonclick="alert(this.style.float);
if(this.style.cssFloat){this.style.cssFloat='left';
}else{this.style.styleFloat='left';
}alert(this.style.float);">測試2</div>