原生JS 獲取非行間樣式寫法 及 style currentStyle getComputedStyle 的區別
阿新 • • 發佈:2018-12-19
1,style:相容各大瀏覽器,獲取不了外部樣式,寫了行內沒有的樣式,返回的是空值 obj.style.attr(獲取樣式) obj.style.attr=“值” 設定屬性值
2,currentStyle:該屬性只相容IE,不相容火狐和谷歌 obj.currentStyle.attr obj.currentStyle[“attr”]
3,getComputedStyle:該屬性是相容火狐谷歌,不相容IE window.getComputedStyle(obj,null)[attr]:有兩個引數,第一個引數是要 獲取誰的樣式,第二個引數隨便傳(必傳)
currentStyle屬性和getComputedStyle屬性不能設定屬值,只能獲取屬性(例如 寬 高 等)
封裝獲取非行間樣式方法:
function getCssStyle(obj, name){
if(obj.currentStyle){
return obj.currentStyle[name]
}else{
return getComputedStyle(obj,false)[name];
}
}
js學習筆記