类似于jquery的css()函数,js封装
CSS函数:css(oDiv , “width” , “200px ”)设置样式
css(oDiv , “width”)获取样式
JS:
Function css(){
If(arguments.length==2){
Return arguments[0].style[arguments[1]]; //获取样式
}else{
Arguments[0].style[arguments[1]] = arguments[2]; //设置样式
}
}
或是
Function css(obj,name,vaule){
If(arguments.length==2){
Return obj.style[name]; //获取样式
}else{
obj.style[name] = vaule; //设置样式
}
}
调用:
Window.onload= function(){
Var oDiv = document.getElementById(“div1”);
css(oDiv , “width” , “200px ”);
css(oDiv , “background” , “blue ”);
}
but:
Obj.style: 只能操作行间样式
兼容IE和高版本chrome操作非行间样式:obj.currentStyle
兼容chrome,FF,IE9操作非行间样式:obj.getComputedStyle(oDiv , null )
If(oDIv .currentStyle){
oDiv.currentStyle.width = “”;
}else{
getComputedStyle(oDiv,false).width= “”;
}