簡體   English   中英

使用Jquery查找被單擊項的類

[英]Find the class of the clicked item using Jquery

我有以下代碼:

    $(".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;
      }
    }); 

if (e.target.tagName.toLowerCase() == "a")的條件語句中,我還要添加“ OR類等於價格”。

我不太確定該怎么做,我似乎找不到被單擊元素的類(在表行中是一個td)。

我試過$(this).class$(this.class)但是都沒有用....

幫助將不勝感激!

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

$(this).hasClass("price");

您可以將兩個測試合並為一個jQuery命令:

if( $(e.target).is('.price, a') ) { .... }

一個測試,如果目標是既具有類price ,或者是a標簽,或者兼而有之。 如果您想測試它是a標簽並且具有price類別,則可以使用以下代碼:

if( $(e.target).is('a.price') ) { .... }

更新:我可能誤解了您的問題。 如果要將第二個測試添加到相同的if測試中,請使用我的代碼。 如果要添加else if則使用.hasClass方法,如其他答案所述。

你可以試試:

$(this).attr("class");

但這將返回您應用於該元素的所有類的名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM