1. 程式人生 > >原生js獲取css樣式

原生js獲取css樣式

版本不兼容 兩個 hang .get .com getc compute ref eid

var ele = document.getElementById("eleid");
1:ele.style.元素名稱

2:var style = window.getComputedStyle ? window.getComputedStyle(ele, null) : ele.currentStyle;   style.元素名稱;

window.getComputedStyle   IE早期版本不兼容,第二個參數可以獲取偽類
currentStyle    IE寫法

以上兩個比較常用。下面鏈接比較全面
https://www.zhangxinxu.com/wordpress/2012/05/getcomputedstyle-js-getpropertyvalue-currentstyle/


原生js獲取css樣式