How to get all the class name of an element in jquery?
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
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:
You might want to check out.hasClass()
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.
}
精彩评论