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

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

问题描述

我不知道发生了什么。



我通过抓住了一个DOM元素的索引, .index(),通过 .get()找到一个匹配的元素,我试图通过 .addClass()



我的控制台正在返回错误: Uncaught TypeError:Object# HTMLLIElement>没有方法'addClass'...这是特别奇怪的,因为我的日志显示的HTML元素很好(http://cloud.dhut.ch/image/2W3S0R3k2h2U)



我错过了什么吗?它不是返回数组或任何东西。感到困惑。



谢谢!



JavaScript:



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

解决方案

你需要将它包装成一个jquery对象。

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

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

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


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

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().

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.

Thanks!

JavaScript:

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

解决方案

You need to wrap it into a jquery object.

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

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天全站免登陆