[英]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.