javascript获取css样式
-
获取行内样式
- ele.getComputedStyle //标准浏览器 IE8以上支持
- ele.currentStyle //支持低版本IE浏览器 IE8及其以下
-
获取非行内样式
- ele.style.attr //只能获取非行内样式
-
封装兼容函数
/**
* 获取元素样式
* @param { ELEMENT } ele 要获取样式得元素
* @param { STRING } style 要获取得样式字符串
* @return { STRING } 获取到得元素得样式
*/
function getStyle(ele, style) {
// 判断 window 里面有没有 getComputedStyle()
if ('getComputedStyle' in window) {
// 标准浏览器 IE8及其以上支持
return window.getComputedStyle(ele)[style]
} else {
// IE 低版本 IE8及其以下 火狐谷歌不支持
return ele.currentStyle[style]
}
}