获取元素正在使用的样式(只读样式,仅IE支持):element.currentStyle.样式名称
获取元素正在使用的样式(只读样式,IE8除外):getComputedStyle(element, null).样式名称
Tips:返回object对象,该对象中封装了当前元素的对应样式,参数一为目标元素,参数二为伪元素,一般传入null即可。
兼容element.currentStyle.styleName 和 getComputedStyle(element, null).styleName的通用方法:
function getElementStyle(elementObj, attrName) {
if (window.getComputedStyle) {
// 常见具备getComputedStyle()的浏览器
return getComputedStyle(elementObj, null)[attrName];
} else {
// IE8等不具备getComputedStyle()的浏览器
return elementObj.currentStyle[attrName];
}
}