.Tang

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
// 点击切换图形验证码 页面加载完后执行,类似window.onload
$(function () {
    var imgCaptcha = $(".img-captcha");
    imgCaptcha.click(function () {
        imgCaptcha.attr("src", "/account/register/img/captcha"+"?random="+Math.random());
    });
});

// 点击发送短信验证码
$(function () {
   var smsCaptcha=$('.short-captcha');  // 获取点击的标签
   function send_sms() {
       var telephone = $('input[name="telephone"]').val();  // 获取input标签name='telephone',用户输入的手机号码
       $.get({
           'url': '/account/register/sms/captcha/',  // 请求url并传data数据给后端(django)request.GET.get("telephone")
           'data': {"telephone": telephone},
           'success': function () {
               var count=30;
               smsCaptcha.unbind("click");  // 点击一次后,取消点击事件
               smsCaptcha.addClass("disabled");  // 点击后增加classname,改变样式
               var timer=setInterval(function () {
                    smsCaptcha.text(count);
                    count--;
                    if (count<0){
                        clearInterval(timer);
                        smsCaptcha.removeClass("disabled");
                        smsCaptcha.text("发送短信验证码");
                        // 时间到了,再次绑定点击事件
                        // smsCaptcha.click(send_sms);  // 第二回点击,快速点击能够请求三次,发三条短信??
                        smsCaptcha.one("click", send_sms)  // 触发一次click事件,每次只发一次短信
                    }
                },1000);
           },
           'fail': function () {
                alert('error')
           }
       })
   }
    // smsCaptcha.click(send_sms);  // 第一回,就算连续点击只能点击一次,发一条短信
    smsCaptcha.one("click", send_sms)  // one(event, function), it's safe to use this way.
});

 切换active类

$(function () {
    // http://127.0.0.1:8000/path/path1/?xx=xxx
    var url1 = window.location.href;
    // http://127.0.0.1:8000/path/path1/
    var url = url1.split('?')[0];
    // http:
    var protocol = window.location.protocol;
    // 127.0.0.1:8000
    var host = window.location.host;
    // http://127.0.0.1:8000
    var domain = protocol + '//' + host;
    // /path/path1/
    var path = url.replace(domain,'');
    var menuLis = $(".menu li");  // li列表
    for(var index=0;index<menuLis.length;index++){
        var li = $(menuLis[index]);
        var a = li.children("a");
        var href1 = a.attr('href');  // /path/path1/?xx=xxx
        var href = href1.split('?')[0];  // /path/path1/
        if(href === path){  // 浏览器url和a标签url比较,相同则表示设置为选中 active
            li.addClass('active');
        }
    }
});

 

posted on 2018-06-28 18:00  .Tang  阅读(951)  评论(0编辑  收藏  举报