jQuery的只是部分工作的点击功能 [英] jQuery only partially working on Click function

查看:119
本文介绍了jQuery的只是部分工作的点击功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此位的jQuery(请原谅可怕的HTML追加! - 这最终将缩短)

I have this bit of jQuery (excuse the horrible html appends - these will be eventually shortened!)

$(function () {
  $(".follow").click(function () {
    var element = $(this);
    var I = element.data("userid");
    var info = 'id=' + I;
    var Something = $('#latestbettors tr[data-userid=' + I + ']').find('td:eq(0)').text();

    $.ajax({
      type: "POST",
      url: "follow.php",
      data: info,
      success: function () {
        $('.follow[data-userid=' + I + ']').fadeOut(200).hide();
        $('.following[data-userid=' + I + ']').fadeIn(200).show();
        if ($('#yourfollowers table tr > td:contains("You arent currently following any bettors")')) {
          $('#yourfollowers table tr:contains("You aren\'t currently following any bettors")').remove();
        }
        if ($('#yourfollowers tr:contains("' + I + '")') && $('#yourfollowers tr > td:contains("' + Something + '")').length < 1) $('#yourfollowers table').fadeIn(200).append("<tr data-userid='" + I + "'><td>" + Something + "</td><td><a href='#'data-userid='" + I + "' class='following'></a><a href='#' data-userid='" + I + "' class='follow' style='display:none'></a></td></tr>");
      }
    });
    return false;
  });
});

$(function () {
  $(".following").click(function () {
    var element = $(this);
    var J = element.data("userid");
    var infos = 'id=' + J;

    $.ajax({
      type: "POST",
      url: "unfollow.php",
      data: infos,
      success: function () {
        $('.following[data-userid=' + J + ']').fadeOut(200).hide();
        $('.follow[data-userid=' + J + ']').fadeIn(200).show();
        $('#yourfollowers tr[data-userid=' + J + ']').fadeOut(200).remove();

        if ($('#yourfollowers table tr').length == 0) {
          $('#yourfollowers table').append('<tr><td>You aren\'t currently following any bettors</td></tr>');
        }
      }
    });
    return false;
  });
});

我有两个表 - 最新投注者和跟随。最新投注表包括所有新签约用户之间的旁边后续按钮(或按用户是否遵守这些规则)的...下表中包含的所有用户正以每旁边以下按钮的用户。

I have two tables - latest bettors and following. The latest bettors table includes all of the newly signed up users with a follow button beside each (or following if the user is following them) ... The following table includes all of the users the user is following with a following button beside each.

当用户点击跟踪按钮,在最新投注'表追加,他们遵循的'下表用户 - 这工作得很好。然而,当用户点击下按钮后,这个在'下面'表 - 没有任何反应?这是为什么?

When the user clicks the follow button in the 'latest bettors' table it appends the user they have followed to the 'following table' - this works well. However when the user clicks on the 'following' button after this in the 'following' table - nothing happens? Why is this?

推荐答案

有关动态生成的元素,事件应该从的元素或文档对象的静态家长,您可以使用一个在代理方法:

For dynamically generated elements, event should be delegated from one of static parents of the element or document object, you can use on or delegate method:

$(document).on('click', '.following', function(){

如果你正在使用jQuery低于1.7的版本,你也可以使用代理方法:

If you are using jQuery below 1.7 version you can also use delegate method:

$(document).delegate('.following', 'click', function(){    

<子>更新者@ 异常

,你也可以使用

$('.following').live('click', function(){    

生活是pcated jQuery的最新版本去$ P $。现在可以工作,如果你使用的是旧版本的jQuery,但它可能无法与您使用最新版本的jQuery的工作。

But live is deprecated in latest versions of jQuery. It may work now if you are using old jQuery version, but it may not work with latest version of jQuery you use.

这篇关于jQuery的只是部分工作的点击功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆