Find the class of the clicked item using Jquery
I have the following code:
$(".perf-row").click(function(e) {
if (e.target.tagName.toLowerCase() == "a") {
pageTracker._trackPageview($(this).attr("rel"));
return true; //link clicked
} else {
window.open($(this).attr("rel"));return false;
}
});
to the conditional statment of if (e.target.tagName.toLowerCase() == "a")
i would like to also add "OR class is equal to price".
I'm not quite sure how to do that, i can't seem to find the class of the clicked element (which will be a td inside th开发者_如何转开发e table row).
i've tried $(this).class
, $(this.class)
but neither work....
Help would be greatly appreciated!
To see whether the element has class .price
, use the $.hasClass()
method.
$(this).hasClass("price");
You could combine both your tests into a single jQuery command:
if( $(e.target).is('.price, a') ) { .... }
That tests if the target is either has the class price
, or is an a
tag, or is both. If you wanted to test that it was an a
tag and had the price
class, you would use this:
if( $(e.target).is('a.price') ) { .... }
Update: I may have misread your question. If you were going to add this second test to the same if
test, then use my code. If you wanted to add a else if
then use the .hasClass
method as explained by the other answers.
you can try:
$(this).attr("class");
but this will return you names of all the classes applied to the element.
精彩评论