注:私はjQueryを学んでいるので、これをより効率的に行うことができれば、共有してください.
フォロー/フォロー解除機能をコーディングしています。ユーザーが別のユーザーをフォローしている部分は機能しますが (以下の最初のスニペットを参照)、ユーザーが "フォロー中" をホバー/マウスオーバーしたときに "フォロー中" を "フォロー解除" に変更したいと考えています。
「フォロー」するコードは次のとおりです (動作します)。
$(document).on('click','a.follow-user',function () {
event.preventDefault();
var action = $(this).attr("action");
var userid = $(this).attr("userid");
var follower_userid = $(this).attr("follower_userid");
$.ajax({
type: "POST",
url: "/follow-user.php",
data: "action=" + action + "&userid=" + userid + "&follower_userid=" + follower_userid,
success: function(data) {
if (data == "FOLLOWED") {
$(".follow-user").html("Following");
$(".follow-user").attr({"action" : "0"});
$(".follow-user").removeClass("follow-user").addClass("following-user");
} else {
$(".follow-user").removeClass(".follow-user").addClass("icon-warning-sign");
}
}
});
});
マウスが「次の」テキストの上にあるときのコードは次のとおりです(機能しません-エラーはありません):
$(".following-user").on("mouseover", function () {
$(this).html("Unfollow")
$(this).attr({"action" : "1"});
}).mouseout(function() {
$(this).html("Following")
$(this).attr({"action" : "0"});
});