我在我的应用程序上做了类似的操作,我使用了以下逻辑:
的HTML:
<a onclick="like($(this))"></a>
js:
function like(_element){ if($(_element).hasClass('unlike')){$.ajax(); //do unlike $(_element).removeClass('unlike'); // this goes inside the success:function(){} of the ajax call}else{ $.ajax(); //do like $(_element).addClass('unlike'); // this goes inside the success:function(){} of the ajax call} }您也可以仅使用1个ajax调用来重构此示例,您的代码将更少



