<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Title</title>
</head>
<body>
<p class="code"></p>
<button>生成验证码</button>
<script>
//验证码函数
function createCode(length) {//验证码长度
var code = "";
for (var i = 1; i <= length; i++) {
code += (parseInt(Math.random() * 10));
}
return code;
}
//输出函数
function deal() {
document.getElementsByClassName("code")[0].innerText = "生成的验证码:" + createCode(4);
}
//事件处理
document.getElementsByTagName("button")[0].onclick = deal;
</script>
</body>
</html>