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