简单的js获取验证码按钮加效果

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>前端ABC-获取验证码</title>
<style type="text/css">
.checkCode {
    cursor: pointer;
    border: 1px solid black;
    text-align: center;
    line-height: 26px;
    width: 115px;
    height: 35px;
}
</style>
<script type="text/javascript">
    var sleep = 30, interval = null;
    window.onload = function ()
    {
        var btn = document.getElementById ('btn');
        btn.onclick = function ()
        {
            if (!interval)
            {
                this.style.backgroundColor = 'rgb(243, 182, 182)';
                this.disabled = "disabled";
                this.style.cursor = "wait";
                this.value = "重新发送 (" + sleep-- + ")";
                interval = setInterval (function ()
                {
                    if (sleep == 0)
                    {
                        if (!!interval)
                        {
                            clearInterval (interval);
                            interval = null;
                            sleep = 30;
                            btn.style.cursor = "pointer";
                            btn.removeAttribute ('disabled');
                            btn.value = "免费获取验证码";
                            btn.style.backgroundColor = '';
                        }
                        return false;
                    }
                    btn.value = "重新发送 (" + sleep-- + ")";
                }, 1000);
            }
        }
    }
</script>
</head>
<body>
    <input class="checkCode" type="button" id="btn" value="免费获取验证码" />
</body>
</html>

欢迎分享本文,转载请保留出处:前端ABC » 简单的js获取验证码按钮加效果

赞 (4)
分享到:更多 ()

发表评论 0