0

注:私は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"});
});
4

3 に答える 3

0

あなたはこれを必要とします:

$(document).on( 'mouseover', '.following-user', function() {
于 2013-03-29T07:01:59.133 に答える
0

following-userドキュメントまたは最も近い静的な親に委任してみてください

$(document).on("mouseover", ".following-user",function () {
于 2013-03-29T07:02:27.270 に答える
0

イベント委任を使用してイベント ハンドラーをバインドします。

$(document).on({
  mouseover: function () {
    $(this).html("Unfollow").attr({
      "action": "1"
    });
  },
  mouseout: function () {
    $(this).html("Following").attr({
      "action": "0"
    });
  }
}, ".following-user");

特に、それらの HTML を変更しているためです。

于 2013-03-29T07:08:23.027 に答える