jQuery count characters and add class

后端 未结 4 1596
予麋鹿
予麋鹿 2021-01-07 04:02

Is it possible to use jQuery to count how many characters are in, for example, an

  • , and if the amount is greater than XX characters, apply a class to
  • 4条回答
    •  甜味超标
      2021-01-07 04:34

      You can get the amount of characters in a string by doing this a.length, where a is a string.

      $('a').each(function(){
       if($(this).text().length > XX){
        $(this).addClass('someClass');
       }
      });
      

    提交回复
    热议问题