<html> <head> <meta charset="utf-8"> </head> <body> <input type="text" id="codeIn"><br> <span id="codebox">AAAA</span> <button type="button" id="codec">看不清换一张</button> </body> <script> let codeIn = document.querySelector("#codeIn"), codebox = document.querySelector("#codebox"), codec = document.querySelector("#codec"); /* queryCode @param:none @rerurn:none */ function queryCode(){ let area = 'akjgf301290987tfhcn29'; let result = ''; for(let i=0; i<4; i++) { let ran = Math.round(Math.random()*area.length-1); result += area[ran]; } codebox.innerHTML = result; } codec.onclick = function(){ queryCode(); } codeIn.onblur = function(){ let val = codeIn.value, code = codebox.innerHTML; if(val.toLowerCase() === code.toLowerCase()) { alert("验证成功"); } else{ codeIn.value = ''; alert("验证失败,请重新输入"); queryCode(); } } </script> </html>