前端学习(465):对象遍历前言

半腔热情 提交于 2020-01-21 18:45:29
<!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;
        }
    }
})();

运行结果

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!