jQuery add class to object with .get() method

扶醉桌前 提交于 2019-12-04 03:29:40

问题


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');


回答1:


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');


来源:https://stackoverflow.com/questions/12362716/jquery-add-class-to-object-with-get-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!