简体   繁体   中英

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 the table row).

i've tried $(this).class , $(this.class) but neither work....

Help would be greatly appreciated!

若要查看元素是否具有.price类,请使用$.hasClass()方法。

$(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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM