所以,这是我的代码
$(".heart.fa").click(function() {
$(this).toggleClass("fa-heart fa-heart-o");
//Wanna check here is its fa-heart or fa-heart-o
var row = $(this).closest("i");
var rowId = row.attr("id");
alert(rowId);
});
<div>
<i id= '".$i."' class='heart fa fa-heart-o'></i>
</div>
想要检查此函数当前是否为 fa-heart-o
或 fa-heart
最佳答案
hasClass 函数可能就是您想要的 https://api.jquery.com/hasclass/
所以你会做这样的事情:
if($(this).hasClass('fa-heart-o')){
//logic for having fa-heart-o
}
else{
//logic for having fa-heart
}
关于javascript - jquery:检查当前切换的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53509495/