我有以下代码:
$(".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 class等于价格”。
我不太清楚如何做到这一点,我似乎无法找到被点击元素的类(这将是表格行中的td)。
我已经尝试了$(this).class
,$(this.class)
但是都没有工作......
非常感谢帮助!
答案 0 :(得分:2)
要查看该元素是否具有类.price
,请使用$.hasClass()
方法。
$(this).hasClass("price");
答案 1 :(得分:0)
您可以将两个测试组合到一个jQuery命令中:
if( $(e.target).is('.price, a') ) { .... }
测试目标是具有类price
,还是a
标记,或者两者都是。如果你想测试它是a
标记并且有price
类,你可以使用它:
if( $(e.target).is('a.price') ) { .... }
更新:我可能误解了您的问题。如果您要将第二个测试添加到相同的if
测试中,请使用我的代码。如果您想添加else if
,请使用.hasClass
方法,如其他答案所述。
答案 2 :(得分:-2)
你可以尝试:
$(this).attr("class");
但这将返回应用于元素的所有类的名称。