jQuery count characters and add class

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

      If you want to target a specific li item (instead of all li elements in your page) you can give it an ID :

    • ...
    • Then if you want to execute your code at page load do something like this :

      $(document).ready(function() {
          var yourElement = $('#myli');
          var charLength = yourElement.text().length;
          if(charLength > xx){
             yourElement.addClass('yourClass');
          }
      });
      

    提交回复
    热议问题