jQuery - 只从一个元素获取第一个类 [英] jQuery - get the first class only from a element
本文介绍了jQuery - 只从一个元素获取第一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
元素如下所示:
<li class="blah active"> ... </li>
jQuery.attr('class')
将返回这两个类。
如何在jQuery中只获得第一个类(在这种情况下为'blah')?
jQuery.attr('class')
will return both classes.
How can I get only the 1st class ('blah' in this case) with jQuery?
推荐答案
将 拆分
添加到array:
You need to split
that into an array:
console.log(jQuery('selector').attr('class').split(' ')[0]);
这篇关于jQuery - 只从一个元素获取第一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文