【问题标题】:Substitution for jQuery live and on替换 jQuery live 和 on
【发布时间】:2014-04-04 04:23:25
【问题描述】:

获得与 live() 相同功能的正确方法是什么,以及 jQuery 中的 on()

【问题讨论】:

  • 手动创建监听器...但是你为什么要这样做呢?
  • 委托()............

标签: jquery bind jquery-1.4


【解决方案1】:

从 jQuery 1.7 开始,.live() 方法已被弃用。使用 .on() 附加事件处理程序。旧版本 jQuery 的用户应该使用 .delegate() 而不是 .live()。

.on() 适用于 jQuery 1.7 及更高版本。如果您有旧版本,请使用:

$("#SomeId").live("click",function(){
    //do stuff;
});

但是你可以使用live(),因为你使用的是jQuery<1.5

【讨论】:

    【解决方案2】:

    使用 delegate https://api.jquery.com/delegate/ 适用于 jQuery 1.4.3+

    【讨论】:

      【解决方案3】:

      您需要使用.delegate

       $( "table" ).delegate( "td", "click", function() {
        alert($(this).html());
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-03
        • 1970-01-01
        • 2013-10-02
        • 2012-04-09
        • 1970-01-01
        相关资源
        最近更新 更多