jQuery - get the first class only from a element

前端 未结 3 1948
不知归路
不知归路 2020-12-01 01:09

The element looks like this:

  • ...
  • jQuery.attr(\'class\') will return both classes.

    相关标签:
    3条回答
    • 2020-12-01 01:52

      You need to split that into an array:

      console.log(jQuery('selector').attr('class').split(' ')[0]);
      
      0 讨论(0)
    • 2020-12-01 01:52
      var first = jQuery.attr('class').split(" ")[0];
      
      0 讨论(0)
    • 2020-12-01 01:54

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

      var firstClass = jQuery('selector')[0].classList[0]
      
      0 讨论(0)
    提交回复
    热议问题