使用jQuery获取数组的最后一个类名 [英] Get the last classname of an array with jquery
问题描述
我想获得带有类名的数组的最后一项.我的代码看起来像这样
i want get the last item of an array with class names. My code looks like this
var getClassName = [];
getClassName = $(this).attr('class').split();
console.log(getClassName);
在控制台中,我成为得到这个答案
In the console i become get this answer
["classname1 classname2"]
我如何获得班级姓氏?
谢谢
推荐答案
正如詹斯格拉姆(Jensgram)所指出的,您快要到了;如果您想保留特定于jQuery的信息,请参阅他的答案.
As jensgram points out, you're nearly there; see his answer for the detail if you want to stay jQuery-specific.
但是您要让浏览器做很多额外的工作,这是您真的不需要jQuery的情况之一:
But you're making the browser do a lot of extra work, this is one of those times you really don't need jQuery:
var getClassName;
getClassName = this.className.split(' ');
console.log(getClassName[getClassName.length-1]);
<所有主流浏览器(可能还有所有次要浏览器)都支持DOM元素的c0>属性.
不过,除非您是在一个紧密的循环中进行此操作,否则$()
和attr
调用的额外开销可能并没有太大关系.
Still, unless you're doing this in a tight loop, the extra overhead of the $()
and attr
calls probably doesn't really matter much.
这篇关于使用jQuery获取数组的最后一个类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!