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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37537113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:26:26  来源:igfitidea点击:

.css() is not a function [jQuery]?

javascriptjqueryhtmlangularjs

提问by KO12

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>

I've created a keyboard control where the goal is to focus on each tile ("tile" directive) in the selectedTiles arrayper keyboard event (shift + right arrow).

我创建了一个键盘控件,其目标是关注selectedTiles array每个键盘事件中的每个 tile(“tile”指令)(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");

        }

When I get a console log of the variable elemI 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我得到了出现在 dom 中的瓦片数组(正如预期的那样)。问题是当我尝试添加一个.attr()函数或.css()函数时,出现错误,指出这些不是函数,我该如何调整?

回答by Dhara Parmar

Use $(elem[curIndex]) instead of elem[curIndex] and Try this:

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

.css() is method of jquery and if you want to use that method you can access element using

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

So it will be:

所以它将是:

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

Now if you want to use javascript to add style to element you can try this:

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

(just an example)

(只是一个例子)

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

回答by Bhojendra Rauniyar

When you use like elem[curIndex], the elem (jQuery wrapper) becomes javascript object. So, you're getting such error as they are not of core javascript method but jquery.

当您使用 like 时elem[curIndex],elem(jQuery 包装器)成为 javascript 对象。所以,你会收到这样的错误,因为它们不是核心 javascript 方法,而是 jquery。

So, you need to wrap it again with jquery:

所以,你需要用jquery再次包装它:

$(elem[curIndex])//now using .css() or .attr() will not throw you errors.