隨機驗證碼: ...
隨機驗證碼:
<!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>