multiple class events together , how to get the current class name
I have multiple class events bind together for a click event ,
i want to know which class is clicked , how can i get current user selected class
$('.class2 , .class3 , .class3').bin开发者_JAVA百科d('click', function () {
location.href = "test.htm";
});
You can use .className
like this:
$('.class2 , .class3, .class3').bind('click', function() {
alert(this.className);
location.href = "test.htm";
});
It can be anywhere from 1 to 3 of those classes though, including other unrelated classes.
Or, since you only actually have 2 if you want to test it using .hasClass()
that's an option as well:
$('.class2 , .class3').bind('click', function() {
var c = $(this).hasClass("class2") ? "class2" : "class3";
alert(c);
location.href = "test.htm";
});
You can use .hasClass()
$('.class1 , .class2 , .class3').bind('click', function() {
if($(this).hasClass('class1')) {
location.href = "test1.htm";
} else if($(this).hasClass('class2')) {
location.href = "test2.htm";
} else if($(this).hasClass('class3')) {
location.href = "test3.htm";
}
});
You could use the hasClass method ala:
if($(this).hasClass('class2'))
Though I'm not sure how many classes you'd look through or what exactly you need it for so it might not be the best route :)
精彩评论