开发者社区> 问答> 正文

在IE下如何锁定input的焦点

$('#nickname').blur(function(event) {
        var $this = $(this);
        var $tip = $this.next('span');
 
           if (this.value.length < 2 || this.value.length > 10) {
                $tip.html('昵称长度为2-10个字');
                this.focus();
                return false;
            }
 
              
    });
 
    $('#password').blur(function(event) {
        var $this = $(this);
        var $tip = $this.next('span');
 
            if (this.value.length < 6 || this.value.length > 15) {
                $tip.html('密码长度为6-15位');
                this.focus();
                return false;
            }
      
    });

我这样写,在chrome下没问题,firefox不管,IE下会两个input之间来回跳,直接到浏览器卡死!!!

展开
收起
a123456678 2016-07-12 10:13:35 1803 0
1 条回答
写回答
取消 提交回答
  • <!DOCTYPE html>
    <html lang="en">
      <head>
        <meta http-equiv="X-UA-Compatible" content="IE=8" />
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>test</title>
     
        <script src="/js/jquery-1.8.3.min.js"></script>
     
        <script>
     
        $(function() {
             
     
            var fna = function(event) {
                var $this = $('.nickname');
                var $tip = $this.next('span');
     
                   if ($this.get(0).value.length < 2 || $this.get(0).value.length > 10) {
                        $tip.html('昵称长度为2-10个字');
                        $('.password').off('blur');
                        $this.get(0).focus();
                        return false;
                    }
                    else{
                        $('.password').on('blur',fnb);
                    }
     
            }
     
            var fnb = function(event) {
                var $this = $('.password');
                var $tip = $this.next('span');
     
                if ($this.get(0).value.length < 6 || $this.get(0).value.length > 15) {
                    $tip.html('密码长度为6-15位');
                    $('.nickname').off('blur');
                    $this.get(0).focus();
                    return false;
                }
                else{
                    $('.nickname').on('blur',fna);
                }
              
            }
     
            $('.nickname').off('blur').on('blur',fna);
     
            $('.password').off('blur').on('blur',fnb);
        })
        </script>
        <!-- Fav and touch icons -->
      </head>
     
        <body>
        <div>
            <input type="text" value="" class="nickname" />
            <span></span>
        </div>
        <div>
            <input type="text" value="" class="password" />
            <span></span>
        </div>
        </body>
    2019-07-17 19:54:41
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载