js中獲取樣式我了解到三種。一種是通過obj.offsetAttr來獲取樣式,通過這種方法來獲取元素的寬高時,如果沒有邊框,可以正確獲取,如果使用邊框?qū)傩詣t會出現(xiàn)問題,為了解決這個問題,可以使用另一種方法,通過getComputedStyle屬性來獲取css樣式(非行間)。還有一種是通style屬性來獲取css樣式(行間)。
/*可以作為一段標(biāo)準(zhǔn)代碼,理解并記憶(個人建議)*/? currentStyle? IE 兼容寫法
?function? ? ? getStyle? ? (obj,attr){?
?if(obj.currentStyle){
? ? ? ? ? ? ? ? return obj.currentStyle[attr];
? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? return getComputedStyle(obj,false)[attr];
? ? ? ? ? ? } }