zoukankan      html  css  js  c++  java
  • angular 调用element的 onfocus onkeydown onblur等事件

    项目里要实现一个input验证通过就切换到下一个input的功能

    当然用jq dom操作很简单  ,大家都懂,现在用 angular,mvc 数据模型控制分离,不想再dom操作怎么办

    以下方法  

    <textarea style="height: 73px;min-height: 73px;resize: none "
              type="text"
              focus-me="item.Bake.FocusNext"
              maxlength="500"
              ng-model="item.Bake.PromoText" />
    

      

    app.directive('focusDir',[ "$timeout","$parse" ,function($timeout,$parse) {
        return {
            link: function($scope, element, attrs) {
                var model = $parse(attrs.focusDir);
                $scope.$watch(model, function(value) {
                   // console.log('value=',value);
                    if(value === true) {
                        $timeout(function() {
                            element[0].focus();
                        });
                    }
                });
                // to address @blesh's comment, set attribute value to 'false'
                // on blur event:
                element.bind('blur', function() {
                   // console.log('blur');
                    $scope.$apply(model.assign($scope, false));
                });
            }
        };
    }] );
    
    //Tip:记得 引入"$timeout","$parse" 
    

      

    这个事focus ,类似的 大家可以用于kedown keyup等等 

    引用:http://stackoverflow.com/questions/14833326/how-to-set-focus-on-input-field

  • 相关阅读:
    setTimeout详解
    【康娜的线段树】
    【[CQOI2016]手机号码】
    【[IOI2014]Wall 砖墙】
    【[1007]梦美与线段树】
    【[POI2010]ANT-Antisymmetry】
    【[HEOI2016/TJOI2016]排序】
    【[SCOI2016]背单词】
    【[HNOI2008]GT考试】
    【[JSOI2007]建筑抢修】
  • 原文地址:https://www.cnblogs.com/Zoes/p/5021611.html
Copyright © 2011-2022 走看看