每日一学
写一个函数随机生成16进制的颜色编码,并在页面显示。
1、页面样式
<input type="text" id="code1" value="" placeholder="随机颜色编码">
<button onclick="getColor()">获取</button>
2、Js生成函数
function getColor() {
var str = "#";
var arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
for(var i = 0; i < 6; i++) {
var num = parseInt(Math.random() * 16);
str += arr[num];
}
document.getElementById("code1").value = str;
}