欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript實(shí)現(xiàn)驗(yàn)證碼案例

 更新時間:2021年10月26日 17:08:43   作者:wait......  
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)驗(yàn)證碼案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)驗(yàn)證碼效果的具體代碼,供大家參考,具體內(nèi)容如下

今天的案例,效果如下:

這個案例的實(shí)現(xiàn)其實(shí)沒有很多難點(diǎn),讓我們一起來看看吧~

html和css的實(shí)現(xiàn),在這里就不做解釋啦,可以比對下面的代碼自己實(shí)現(xiàn)一下,注意一下細(xì)節(jié)就好了

接著咱們來看看js的實(shí)現(xiàn):

我們需要做到的有兩點(diǎn):

1、實(shí)現(xiàn)驗(yàn)證碼的隨機(jī)產(chǎn)生,使其在頁面刷新和點(diǎn)擊更換時能夠生成
2、實(shí)現(xiàn)輸入字符串和驗(yàn)證碼的比較

第一點(diǎn)呢,我們需要用到for循環(huán)Math.round(Math.random()*n),使得在每一次循環(huán)中可以產(chǎn)生隨機(jī)數(shù)字

第二點(diǎn)呢,我們只需要通過input.value獲得用戶輸入的字符串,然后將其與之前隨機(jī)產(chǎn)生的字符串進(jìn)行比較即可(使用===)

其他的細(xì)節(jié)可以去代碼中查看哦

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container{
            width: 400px;
            height: 100px;
            margin:100px auto;
            background-color: hsla(180, 73%, 78%, 0.199);
            border-radius: 20px;
            text-align: center;
            padding: 20px;
        }
        #check{
            display: inline-block;
            width: 100px;
            height: 30px;
            text-align: center;
            background-color: rgba(128, 128, 128, 0.158);
            color:blue;
            font-size:26px;
            font-style: italic;
            letter-spacing: 2px;
            font-family:Arial, Helvetica, sans-serif;
            margin-bottom: 10px;
        }
        .ma{
            margin-bottom: 12px;
        }

    </style>
</head>

<body>
    <div class="container">
        <div>
            <span id="check">adf34y</span>
            <a href="#" id="checka">看不清換一張</a>
        </div>
        <div class="ma">
            <label for="zheng">驗(yàn)證碼</label>
            <input type="text" id="zheng">
        </div>
        <button id="btn">確定</button>
    </div>

    <script>
        let sum=[0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f'];
        var check=document.getElementById("check");
        var code;
        
        function fun(){
            let str="";
            for(let i=0;i<6;i++){
                let pos=Math.round(Math.random()*15);//注意這個寫法,取隨機(jī)數(shù)
                str+=sum[pos];
            }
            check.innerHTML=str;
            return str;
        }

        window.onload=function(){
            code=fun();
        }

        let checka=document.getElementById("checka");
        checka.onclick=function(){
            code=fun();
        }
        
        let btn=document.getElementById("btn");
        btn.onclick=function(){
            let t=document.getElementById("zheng").value;
            console.log(t)
            if(t==code){
                alert("正確");
                code=fun();
                document.getElementById("zheng").value="";
            }
            else{
                alert("錯誤");
                document.getElementById("zheng").value="";
            }
        }
        

    </script>
</body>

</html>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論