函数封装
//调用外部封装好的js库即可Utils:
var Utils=(function () {
return {
setStyle:function (elem,style) {
for(var prop in style){
elem.style[prop]=style[prop];
}
},
randomColor:function (alpha) {
if(alpha>1 || isNaN(alpha) || alpha<0){
alpha=1;
}
var color="rgba(";
for(var i=0;i<3;i++){
color+=parseInt(Math.random()*256);
color+=",";
}
color+=alpha+")";
return color;
}
}
})();
//函数调用
var div0=document.getElementById("div0");
Utils.setStyle(div0,{
width:"100px",
height:"100px",
backgroundColor:Utils.randomColor()
})