gpt4 book ai didi

javascript - .css() 不是函数 [jQuery]?

转载 作者:太空狗 更新时间:2023-10-29 13:36:31 24 4
gpt4 key购买 nike

我有以下自定义标签/指令:

<tile class="ng-scope gridster-item" tilevalue="1" gridster-item="tile" row="0" col = "0" ng-repeat="tile in selectedTiles"> </tile>

我创建了一个键盘控件,其目标是关注 selectedTiles array 中的每个图 block (“图 block ”指令)每个键盘事件 (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 中的图 block 数组(正如预期的那样)。问题是当我尝试添加 .attr()功能或 .css()函数,我收到错误提示这些不是函数,我该如何调整?

最佳答案

使用 $(elem[curIndex]) 而不是 elem[curIndex] 并试试这个:

.css() 是 jquery 的方法,如果您想使用该方法,您可以使用

访问元素

所以会是:

$(elem[curIndex]).css("border-color", "yellow");

现在如果你想使用 javascript 给元素添加样式你可以试试这个:

(只是一个例子)

var myElement = document.querySelector("#superman");
myElement.style.backgroundColor = "#D93600";

关于javascript - .css() 不是函数 [jQuery]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37537113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com