jQuery count characters and add class

后端 未结 4 1566
予麋鹿
予麋鹿 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:08

      You could do something like this:

      // The number of characters
      var XX = 100;
      
      $('li').filter(function() {
          return $(this).text().length > XX;
      }).addClass('someClass');
      

    提交回复
    热议问题