js random codes

Random verification code:

<!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 > 
            // verify the number of characters required code 
            var NUM = . 4 ;
             // click replace the verification code 
            function AUTH_CODE () {
                 // Get the id box element 
                var box = $ ( " AUTH_CODE " );
                 // random changing character 
                box.innerHTML = randstr ();
                 // Get box in the span element, and the color change 
                for ( var I = 0 ; I < NUM; I ++ ) {
                    var span=box.getElementsByTagName("span")[i];
                    span.style.color=randcolor16();
                }    
            }
            // Get function element by ID ----------- ----------- 
            function $ (idname) {
                 return document.getElementById (idname);                
            }
            // characters randomly selected function --------------- ------------ 
            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+")"                    
            }
            // random color hex ----------- function ------------------ 
            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>

 

Guess you like

Origin www.cnblogs.com/gust-lq/p/11374091.html