forked from WangJia-mm/JavaScript201708
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-code.html
More file actions
79 lines (71 loc) · 2.01 KB
/
Copy path3-code.html
File metadata and controls
79 lines (71 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>验证码</title>
<style>
* {
margin: 0;
padding: 0;
}
.codeBox {
margin: 20px auto;
width: 100px;
height: 35px;
line-height: 35px;
font-size: 18px;
background: lightcoral;
text-align: center;
cursor: pointer;
}
</style>
</head>
<body>
<div class="codeBox" id="codeBox"></div>
<script>
var codeBox = document.getElementById('codeBox');
var codeArea = '0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
/*
* 思路三:康晶
* 我每一次把获取的随机索引存储起来,下一次获取最新的索引,看一下是否和之前的索引重复,如果重复,就从新在获取一个索引
*/
// function queryRandomCode() {
// var ary = [];
// var result = '';
// while (result.length < 4) {
// var ran = Math.round(Math.random() * 61);
// if (ary.indexOf(ran) === -1) {
// ary.push(ran);
// result += codeArea[ran];
// }
// }
// codeBox.innerHTML = result;
// }
var ary = [];
function queryDistinctRan() {
var ran = Math.round(Math.random() * 61);
// while (ary.indexOf(ran) !== -1) {
// ran = Math.round(Math.random() * 61)
// }
for (var i = 0; i < ary.length; i++) {
if (ran === ary[i]) {
ran = Math.round(Math.random() * 61);
i--;
}
}
ary.push(ran);
return ran;
}
function queryRandomCode() {
var result = '';
for (var i = 0; i < 4; i++) {
var ran = queryDistinctRan();
result += codeArea[ran];
}
codeBox.innerHTML = result;
}
queryRandomCode();
codeBox.onclick = queryRandomCode;
</script>
</body>
</html>