【问题标题】:jQuery.ajax new element checkjQuery.ajax 新元素检查
【发布时间】:2012-11-23 13:52:15
【问题描述】:

我正在尝试使用 ajax 登录网页。我有这个:

    $.ajax({
       type: "POST",
       url: "ajax/login.php",
       data: dataString,               
       success: function(answer) {
           $('#loging').html(answer);           
       }      
    });

它工作正常,但是:答案变量是这样的:

    Hi user, <span id="logout_button">Logout</span>

我可以看到“注销”按钮,但我的浏览器不知道这里有任何 span id="logout_button"。我有与#logout_button 绑定的注销代码,但它不起作用,因为我的浏览器对此按钮一无所知。当我刷新页面时,注销工作正常。

那么,有什么方法/功能可以对 jQuery 说“嗨,伙计,请检查文档上的新元素”?

【问题讨论】:

  • 请定义“工作正常”和“不工作”

标签: jquery ajax login refresh element


【解决方案1】:

是的,您可以使用 .delegate() 代替 .bind()

$('body').delegate('#logout_button', 'click', function() {
    //here the code
});

【讨论】:

    【解决方案2】:

    对于 jquery 1.7+,最好使用“on”

    $("body").on("click", "#logout_button", function() {
        //here the code
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-14
      • 2022-10-13
      • 2020-02-21
      • 1970-01-01
      • 2019-12-18
      • 1970-01-01
      相关资源
      最近更新 更多