开发者

jQuery - get the first class only from a element

开发者 https://www.devze.com 2023-01-06 06:30 出处:网络
The element looks like this: <li 开发者_JS百科class=\"blah active\"> ... </li> jQuery.attr(\'class\') will return both classes.

The element looks like this:

<li 开发者_JS百科class="blah active"> ... </li>

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]);


var first = jQuery.attr('class').split(" ")[0];


You can use the following to get the first class only from an element:

var firstClass = jQuery('selector')[0].classList[0]
0

精彩评论

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