开发者

jQuery - if two classes - get the first one

Example:

Here we have <p class="tab1 current"></p>

How can I get only the first class?

var GetFirstClass = $('p').attr('class').filter(':first'); ??

Any help开发者_开发问答 much appreciated.


Use JavaScript's split function:

$('p').attr('class').split(' ')[0]

Demo


$('p').attr('class').split(' ')[0]


You can split the string based on the space.


$('<div class="a b"/>').attr('class').split(' ')[0]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜