JavaScript实用的例子

---恢复内容开始---

1.发送验证码

<input id="send" type="button" value="发送验证码">

<script>  
    var times = 60, // 时间设置60秒
    timer = null;
    document.getElementById('send').onclick = function () {
     // 计时开始
     timer = setInterval(function () {
     times--;
     if (times <= 0) {  
     send.value = '发送验证码';
     clearInterval(timer);
     send.disabled = false;
     times = 60;
     } else {
     send.value = times + '秒后重试';
     send.disabled = true;
     }
     }, 1000);
    }
    var times = 60,
        timer = null;
    $('#send').on('click', function () {
    var $this = $(this);
    // 计时开始
    timer = setInterval(function () {
    times--;
     if (times <= 0) {  
        $this.val('发送验证码');
        clearInterval(timer);
        $this.attr('disabled', false);
        times = 60;
        } else {
            $this.val(times + '秒后重试');
            $this.attr('disabled', true);
        }
        }, 1000);
    });
</script>

 

2.输出今天的日期,以YYYY-MM-DD的方式,比如今天是2018年5月21日,则输出2018-05-21

var date = new Date();
    var dateYear = date.getFullYear();
    var dateMonth = date.getMonth() + 1;
    dateMonth = dateMonth < 10 ? '0'+dateMonth : dateMonth;
    var dateDays = date.getDate();
    dateDays = dateDays < 10 ? '0'+dateDays : dateDays;
    console.log(dateYear + '-' + dateMonth + '-' + dateDays);

 

3.用js来判断浏览器版本,以及手机终端

 js来判断浏览器版本

var ipspan =document.querySelector(".span");
    
    function inspect(){
        var userAgent=navigator.userAgent;

        // 火狐浏览器
        if(userAgent.indexOf("Firefox") >-1){
            return "Firefox";
        }
        // 谷歌浏览器
        if(userAgent.indexOf("Chrome") >-1){
            return "Chrome";
        }
        // IE
        if(userAgent.indexOf("IE") >-1){
            return "IE";
        }
    }

    ipspan.onclick=function(){
        document.querySelector(".seebro").innerHTML =inspect();
    }

 js来判断手机终端,是安卓还是苹果

 (function(){
        var u = navigator.userAgent;
        var isAndroid =u.indexOf("Android") > -1 || u.indexOf("Adr") > -1; //Android终端  Google 浏览器默认为 Android 端
        var isIOS =!!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //IOS终端

        console.log(isAndroid);
        console.log(isIOS);
    }()) 

 

4.

 

---恢复内容结束---

posted @ 2018-05-21 14:07  三行情书愿你有梦  阅读(263)  评论(0编辑  收藏  举报