第一种方法(生成三位数随机码)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
#code{
width: 100px;
height: 50px;
background-color: lightblue;
font-size: 44px;
letter-spacing: 5px;
}
</style>
</head>
<body>
<script>
function createRandCode(){
var chars=['a','b','c','1','2','3'];
var randcode="";
for(var i=0;i<3;i++){//3位随机码
var randpos =Math.floor(Math.random() * chars.length); //视频修改,以这个为准
randcode+= chars[randpos];
}
document.getElementById("code").innerHTML=randcode;
}
</script>
<div id="code"></div>
<button οnclick="createRandCode()">验证码</button>
</body>
</html>
第二种方法(生成三位数随机码)
<!DOCTYPE html>
<html>
<head>
<mata charset="UTF-8">
<title>Document</title>
<style>
#code{
width: 100px;
height: 50px;
background-color: lightbule;
font-size: 44px;
letter-spacing: 5px;
}
</style>
</head>
<body>
<script>
function createRndCode(){
var chars =['a','b','c','1','2','3']
var randCode ="";
for(var i=0;i<3;i++){
var randPos = Math.floor(Math.random()*(chars.length-1));
randCode += chars[randPos]
}
document.getElementById('code').innerHTML = randCode;
}
</script>
<div id="code"></div>
<button οnclick="createRndCode">验证码</button>
</body>
</html>