<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>对象遍历</title>
<script src="./js/common.js"></script>
</head>
<body>
<div id="div0"></div>
<script>
var div0=document.getElementById("div0");
// 自执行函数
Util.setStyle(div0,{
width:"100px",
height:"100px",
backgroundColor:Util.randomColor()
})
</script>
</body>
</html>
common.js
var Util=(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;
}
}
})();
运行结果
来源:CSDN
作者:你知道歌谣吗?
链接:https://blog.csdn.net/weixin_43392489/article/details/104053498