开发者

How to get all the class name of an element in jquery?

开发者 https://www.devze.com 2023-03-06 21:00 出处:网络
I have some li\'s li class=\"views-row views-row-2 views-row-even\" li class=\"views-row views-row-3 views-row-odd\"

I have some li's

 li class="views-row views-row-2 views-row-even"  
 li class="views-row views-row-3 views-row-odd"  
 li class="views-row views-row-4 views-row-even"  
 li class="views-row views-row-5 views-row-odd"  

Now on hover i am adding a class selected to corresponding li. Suppose i hover on first li element, selected classes get added to this li. So i will have

li class="views-row views-row-2 views-row-even selected". Now how do i get the views-row-2 class of this selected li.Similarly if second li is selected i need views-row-3 class a开发者_JAVA技巧nd so on. Please help


Several options, but this is a good one:

views_row = $("li.selected").attr("class").match(/(views\-row\-[0-9]+)/)[0];


You haven't mentioned what's special about the view-rows-2 class:

  • Do you already know which class you want?
  • Will it always be this class?

With what information you've given, here are my suggestions:

  1. You might want to check out .hasClass()

  2. If you want all classes for an element, see Get class list for element with jQuery

So if you want to get classes based on a prefix (which in your case seems to be views-row-, you could use what's suggested in the answers to these SO question: jQuery - Get a element class based on a prefix and jQuery selector regular expressions


you can use indeOf() something like this...suppose you have that selected class as you have written..

if(class.indexOf('views-row-2')!=-1){
    //do what ever you want to do, you can use sbstr() to get your class name too.
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消