<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function getRandom(){
var r=parseInt(Math.random()*255);
var g=parseInt(Math.random()*255);
var b=parseInt(Math.random()*255);
var a=Math.random().toFixed(2); //保留到小数点后两位
var color1=‘rgb(‘+r+‘,‘+g+‘,‘+b+‘)‘;
var color2=‘rgb(‘+r+‘,‘+g+‘,‘+b+‘,‘+a+‘)‘;
return {
rgb:color1,
rgba:color2
}
}
var color=getRandom();
console.log(color);
</script>
</body>
</html>
原文:https://www.cnblogs.com/sjl9280/p/13205998.html