我有以下自定义标签/指令:
<tile class="ng-scope gridster-item" tilevalue="1" gridster-item="tile" row="0" col = "0" ng-repeat="tile in selectedTiles"> </tile>
我创建了一个键盘控件,其目标是在每个键盘事件(shift + right arrow
)之后,将焦点放在selectedTiles array
中的每个"tile"指令上。
// Keyboard Tile Navigation
//Starting pos
var curIndex = -1
//keyboard control
$(document).keydown(function(event) {
if (event.shiftKey && event.which === 39) {
console.log("right");
focusNext();
}
});
function focusNext() {
focusTile(+1);
}
function focusTile(delta) {
var visibleTiles = $scope.selectedTiles;
var elem = $("[gridster-item='tile']");
curIndex = curIndex + delta;
el = elem[curIndex];
el.attr('tabindex', -1).focus();
el.css("border-color", "yellow");
}
当我获取变量
elem
的控制台日志时,我会得到出现在dom中的瓷砖数组(这是预期的)。问题在于,当我尝试添加.attr()
函数或.css()
函数时,我会收到错误,指出这些不是函数。我该如何调整?
$(el).attr('tabindex'...)
而不是el.attr('tabindex'...)
,因为在使用任何jQuery方法(如.attr()
或.css()
)之前,你需要一个jQuery包装器。 - Kartikeya Khosla