css()获取样式当前值和设置样式的简单封装

获取CSS样式或设置CSS样式,兼容IE,参数一:元素对象,参数二:属性名,参数三(可选):属性值
 
 
function css() {
    if (arguments.length == 2) {
        //获取样式
        if (getComputedStyle(arguments[0], false)) {
            //标准浏览器
            var attr = arguments[1];
            return getComputedStyle(arguments[0], false)[attr];
        } else {
            //ie8-
            var attr = arguments[1];
            return arguments[0].currentStyle[attr];
        }
    } else if (arguments.length == 3) {
        //设置样式 box.style.display = 'none'
        var attr = arguments[1];
        arguments[0].style[attr] = arguments[2];
    }
}

猜你喜欢

转载自www.cnblogs.com/muyun123/p/11503487.html