jQuery 使用 .get() 方法将类添加到对象 [英] jQuery add class to object with .get() method

查看:38
本文介绍了jQuery 使用 .get() 方法将类添加到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遇到奇怪的事情,我不确定发生了什么.

running into a weird thing and I'm not sure what's going on.

我通过 .index() 获取了一个 DOM 元素的索引,通过 .get() 找到了一个匹配的元素,我正在尝试添加一个通过 .addClass().

I've grabbed the index of a DOM element via .index(), found a matching element via .get() and I'm trying to add a class to it via .addClass().

我的控制台返回错误:Uncaught TypeError: Object # has no method 'addClass'"...这特别奇怪,因为我的日志显示 HTML 元素很好(http://cloud.dhut.ch/image/2W3S0R3k2h2U)

My console is returning the error: "Uncaught TypeError: Object #<HTMLLIElement> has no method 'addClass'"... which is especially odd because my Log shows the HTML element just fine (http://cloud.dhut.ch/image/2W3S0R3k2h2U)

我错过了什么吗?它不会以数组或任何形式返回.困惑.

Am I missing something? It's not returning in an array or anything. Confused.

谢谢!

JavaScript:

JavaScript:

nFLi.get(active).addClass('active');

推荐答案

你需要把它包装成一个 jquery 对象.

You need to wrap it into a jquery object.

$(nFLi.get(active)).addClass('active');

或者你可以使用 .eq 方法代替 .get,它返回一个 jquery 对象而不是原始的 HTMLElement.

Or you could use .eq method instead of .get, which returns a jquery object instead of original HTMLElement.

nFLi.eq(active).addClass('active');

这篇关于jQuery 使用 .get() 方法将类添加到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆