项目里要实现一个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(‘focusMe‘, function($timeout, $parse) { return { //scope: true, // optionally create a child scope link: function(scope, element, attrs) { var model = $parse(attrs.focusMe); 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)); }); } }; });
这个事focus ,类似的 大家可以用于kedown keyup等等
引用:http://stackoverflow.com/questions/14833326/how-to-set-focus-on-input-field
angular 调用element的 onfocus onkeydown onblur等事件
原文:http://www.cnblogs.com/Zoes/p/5021611.html