lixiaojuan12 / Summary-of-code

手写代码爽起来~~~
0 stars 0 forks source link

19. 随机验证码 #19

Open lixiaojuan12 opened 4 years ago

lixiaojuan12 commented 4 years ago
//随机验证码:数字和字母组合,(四个数字和字母)
/* 
=> 1先把验证码准备好
=> 2随机获取相应的索引值
=> 3获取元素
*/
var code = document.getElementById("code");
var btn = document.getElementById("btn");
code.innerHTML = getCode();
btn.onclick = function () {
    code.innerHTML = getCode();
}
function getCode() {
    var str = "qwertyuiopasdfghjklzxcvbnm" + "QWERTYUIOPASDFGHJKLZXCVBNM" + "0123456789";
    var result = "";
    //==> 索引值的范围:0---61
    while (result.length < 4) {
        var index = Math.round(Math.random() * 61);
        var item = str[index];
        if (result.indexOf(item) == -1) {
            result += item;
        }
    }
    return result;
}