function setGetStyle(obj,st){
if(st instanceof Array){
//instanceof:判断一个实例化是否属于某一对象;
var getCss='';
for(var i=0;i<st.length;i++){
obj.currentStyle ? getCss+=obj.currentStyle[st[i]] : getCss+=window.getComputedStyle(obj,null)[st[i]];
}
return getCss;
}else{
for(var key in st){
if(isNaN(st[key])){
obj.style[key]=st[key];
}else{
obj.style[key]=st[key]+'px';
}
}
}
}
//调用方法
console.log(setGetStyle(box,['width']));//获取样式
setGetStyle(box,{color:'red'});//添加设置修改样式
网友评论