How to get elements of specific class starting with a given string?

后端 未结 11 853
面向向阳花
面向向阳花 2021-02-03 21:19

I have a list of elements that have multiple classes, for example:


         


        
11条回答
  •  闹比i
    闹比i (楼主)
    2021-02-03 21:38

     // Only select input which have class 
     $('input[class]').click(function(){  
       var myClass;
       // classNames will contain all applied classes
       var classNames = $(this).attr('class').split(/\s+/);  
    
         // iterate over all class  
         $.each(classNames, function(index, item) {
            // Find class that starts with btn-
            if(item.indexOf("btn-") == 0){
              // Store it
              myClass = item;
            }
         });
    
      });
    

    Live Demo

提交回复
热议问题