Get first and last class with jQuery

后端 未结 7 1313
一整个雨季
一整个雨季 2021-02-09 02:58

Might be a newbie question. I have a code line like this:

I need to get each class for itself.

7条回答
  •  醉话见心
    2021-02-09 03:35

    var classes = $(this).attr("class").split(/\s/);
    
    classes[0] === 'template'
    classes[1] === 'active'
    

    If there are more than two classnames and you only want to get the first & last (that was your question) you can call:

    classes[0] = first class
    classes[classes.length -1] = last class
    

提交回复
热议问题