首页 > Web开发 > 详细

js随机验证码

时间:2019-08-18 23:24:59      阅读:190      评论:0      收藏:0      [点我收藏+]

随机验证码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            *{margin:0;padding:0;}
            #auth_code{width:200px; 
                       height:100px; 
                       border:1px solid #ccc;
                       margin:200px auto 0;
                       text-align:center;
                       line-height:100px;
                       font-size:50px;
                       letter-spacing:5px;
                       cursor:pointer;}
        </style>
    </head>
    <body>
        <div id="auth_code" onclick="auth_code()"></div>
        <script>
            //验证码内需要字符个数
            var num=4;
            //点击更换验证码
            function auth_code(){
                //获取id为box元素
                var box=$("auth_code");
                //随机改变字符
                box.innerHTML=randstr();
                //获取box里span元素,并换颜色
                for(var i=0;i<num;i++){
                    var span=box.getElementsByTagName("span")[i];
                    span.style.color=randcolor16();
                }    
            }
            //获取元素通过ID-----------函数-----------
            function $(IdName){
                return document.getElementById(IdName);                
            }
            //字符随机选取---------------函数------------
            function randstr(){
                var all = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z];
                var words="";
                for(var i=0;i<num;i++){
                    var word=all[parseInt(Math.random()*all.length)];
                    words+="<span>"+word+"</span>";
                }
                return words;
            }
            //随机颜色rgb------------函数----------------
            function randcolor(){
                var color= parseInt(Math.random()*255);
                var color1= parseInt(Math.random()*255);
                var color2= parseInt(Math.random()*255);
                return "rgb("+color+","+color1+","+color2+")"                    
            }
            //随机颜色十六进制-----------函数------------------
            function randcolor16(){
                var color="#";
                var color16num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f]
                for(var i=1; i<=6;i++){
                    color+=color16num[parseInt(Math.random()*color16num.length)]
                }
                return color;
            }
            window.onload = auth_code();
        </script>
    </body>
</html>

 

js随机验证码

原文:https://www.cnblogs.com/gust-lq/p/11374091.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!