【发布时间】:2015-11-03 17:15:33
【问题描述】:
我的html代码:
<div ng-app="app">
<form>
<div class="form-group">
<label >Username</label>
<input focus type="text" class="form-control" id="loginUserName" ng-model="userForm.crn" required/>
</div>
<div class="form-group">
<label >Password</label>
<input focus type="password" class="form-control" id="loginPwd" ng-model="userForm.password" required/>
</div>
</form>
我的js代码:
var app = angular.module('app', []); app.directive('focus', function () {
return {
restrict: 'A',
link: function ($scope, elem, attrs) {
elem.bind('keydown', function(e) {
var code = e.keyCode || e.which;
if (code === 13) {
console.log(elem);
elem.next()[0].focus();
//e.preventDefault();
//elem.next().focus();
}
});
}
}
})
1:当点击输入按钮时,此代码不关注下一个元素。表明
nextElementSibling: null 和 nextSibling: console.log(elem) 上的文本
2:但是当我删除表单标签中的标签标签和div时,它开始工作,因为它专注于下一个元素。
所以,问题是如何在不删除 div 和标签的情况下使其工作。为什么 nextElementSibling 会为空?
【问题讨论】:
-
chrome 中的工作文件:jsfiddle.net/Y2XLA/216
-
感谢您的快速回复,我根据您给出的 jsfiddle 链接构建了我的 jsfiddle。但就我而言,我需要关注其他 div 之后的下一个元素
标签: javascript jquery html css angularjs