js中实现验证码功能

<!doctype html>
<html lang="zh">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus?">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
  <script src="jquery.js"></script>
  <script>
        $(function(){
            changeCheckCode();

            $("#checkCode").click(function(){
                changeCheckCode();

            });
        }); 

        

        function changeCheckCode(){
            var valueArray=new Array('qwertyuioplkjhgfdsazxcvbnm','QWERTYUIOPLKJHGFDSAZXCVBNM','1234567890');
            var colorArray=new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
            var color="#";
            var colorArrayLength=colorArray.length;
            var value="";
            for(var i=0;i<4;i++){
                var valueIndex=valueArray[Math.floor(Math.random()*3)];
                value=value+valueIndex.substr((Math.floor(Math.random()*(valueIndex.length))),1);
            }
            console.log("value的值为:"+value);
            
            for(var j=0;j<6;j++){
                color=color+colorArray[Math.floor(Math.random()*colorArrayLength)];
            }
            console.log("color的值为:"+color);
            $("#checkCode").css("background-color",color).text(value);

        }
  </script>
  <style>
        #checkCode{
            width:80px;
            height:40px;
            display:table-cell;
            text-align:center;
            vertical-align:middle;
            color:white;
            font-size:16px;
            font-weight:bold;
            opacity:0.5;
            letter-spacing:5px;
            text-decoration:line-through;
        }
  </style>
 </head>
 <body>
 <div id="checkCode"></div>
 <hr>
 
  
 </body>
</html>


备注:

Math.round(number)      //小数进行四舍五入

Math.floor(number)         //舍弃小数位,向下取整

Math.ceil(number)           //若存在小数,则进位。向上取整

效果图如下:



 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值