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 回答