css函数

用JavaScript来获取css标签属性信息

function getStyle (obj,attr) {
         if(obj.currentStyle){
                 return obj.currentStyle[attr];
         }
         else {
                 return getComputedStyle(obj,false)[attr];
         }
 }

因为有兼容性问题,所以要在currentStyle和getComputedStyle中设置判断。

其中obj=标签名;attr=属性名,属性名字不可以是复合属性,如:background,要获取背景颜色只能用background-Color; 

用JavaScript来设置css标签属性

function css (obj,attr,value) {
        if(arguments.length==2) {
                return getStyle(obj,attr);
        }
        else if(arguments.length==3) {
                obj.style[attr]=value;
        }

如果传入的值为3个,则为设置属性,传入的值为2个则为获取属性;

value=要设置属性attr的值 

你可能感兴趣的:(css函数)