js获得当前元素的样式

时间:2024-04-21 17:17:18

利用currentStyle()和ComputedStyle()

1 通常用法:

balance为类名

window.getComputedStyle(document.querySelector(".balance"), null).backgroundColor;

window.getComputedStyle(document.querySelector(".balance"), null).fontSize;

2 封装一个函数:

function getstyle(obj, key) {

if (obj.currentStyle) {

return  obj.currentStyle.key;

} else {

Return  getComputedStyle(obj, mull).key;

}

}

封装进getstyle函数