开发者

jQuery - if two classes - get the first one

开发者 https://www.devze.com 2023-03-17 23:10 出处:网络
Example: Here we have <p class=\"tab1 current\"></p> How can I get only the first class? var GetFirstClass = $(\'p\').attr(\'class\').filter(\':first\'); ??

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

精彩评论

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