我们在注册某个账号时,页面上经常会提示让输入验证码的情况,今天就列举两个在页面生成验证码的方法
方法一:用数组存验证码
要求:每次刷新页面,都会输出一个六位数的验证码 (字母和数字组成),且每次 刷新验证码都随机变色
css代码:
<style type="text/css">
#mycode{
width: 150px;
height: 30px;
line-height: 30px;
font-size: 26px;
font-weight: bold;
text-align: center;
border: 1px solid red;
float: left;
}
#change{
width: 100px;
height: 30px;
cursor: pointer;
background-color: #ADD8E6;
border-style: none;
margin-left: 10px;
float: left;
}
</style>
html代码:
<div id="mycode"></div>
<button id="change" onclick="downChange()">换一个试试</button>
js代码:
<script type="text/javascript">
//通过class类名获取创建的div元素
var mycode = document.getElementById("mycode");
function createCode(){
//创建验证码函数
//定义一个数组存26个字母
var arrLetter = ['a','b','c'