.css()不是函数[jQuery]? [英] .css() is not a function [jQuery]?
问题描述
我有以下自定义标记/指令:
I have the following custom tag/directive :
<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"指令).
I've created a keyboard control where the goal is to focus on each tile ("tile" directive) in the selectedTiles array
per keyboard event (shift + right arrow)
.
// 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()
函数时,出现错误消息,指出这些不是函数,该如何调整?
When I get a console log of the variable elem
I get the array of tiles that appear in the dom (as is expected). The problem is when I try to add a .attr()
function or a .css()
function, I get the error stating these are not functions, how can i adjust?
推荐答案
使用$(elem [curIndex])代替elem [curIndex],然后尝试以下操作:
Use $(elem[curIndex]) instead of elem[curIndex] and Try this:
.css()是jquery的方法,如果要使用该方法,则可以使用
.css() is method of jquery and if you want to use that method you can access element using
它将是:
$(elem[curIndex]).css("border-color", "yellow");
现在,如果您想使用javascript为元素添加样式,则可以尝试以下操作:
Now if you want to use javascript to add style to element you can try this:
(只是一个例子)
var myElement = document.querySelector("#superman");
myElement.style.backgroundColor = "#D93600";
这篇关于.css()不是函数[jQuery]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!