JS:圖片驗證碼
阿新 • • 發佈:2019-02-17
條件:事先需要做成圖片
原理:隨機調取圖片
案例:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>隨機生成登入圖片驗證碼</title> </head> <body> <table> <tr> <div id='div1'></div> </td> </tr> </table> </body> <script language="javascript"> var str = ""; var img = ""; var strsource = ['是', '天', '日', '科', '的', '會', '更', '好', '創', '新']; for(var i = 0; i < 4; i++) { var n = Math.floor(Math.random() * strsource.length); str = str + strsource[n]; img = img + "<img src='Images/checkcode/" + n + ".gif' width='19' height='20'> "; //圖片路徑 div1.innerHTML = img; //設定div1的輸出 } </script> </html>
執行: