【问题标题】:jQuery: Attach additional functions to existing event handler using $.delegatejQuery:使用 $.delegate 将附加功能附加到现有事件处理程序
【发布时间】:2012-05-07 17:51:45
【问题描述】:

我的组织有一个由多个应用程序共享的标准 jQuery/JavaScript 框架。在这个框架中,我们为 jQuery 的 datepicker 功能设置了一个基本标准,可以与动态加载的元素一起使用:

$(document).delegate("input.calendar", "click, focus", function() { 
    myAppDatepicker = $(this).datepicker({
        changeYear: true,
        dateFormat: "yy-mm-dd"
    });
});

我想做这样的事情:

$(document).delegate("input.calendar", "click, focus", function() { 
    myAppDatepicker.datepicker("option", "maxDate", 0);
});

向日期选择器添加其他选项。这个方法不起作用——我猜是因为第二个委托侦听器正在替换第一个侦听器中的处理程序,我想在其中附加其他选项。

目前,这会引发“myAppDatepicker is not defined”错误。

我可以通过 Firebug 中的 $.bind 来做到这一点,但它不适用于动态加载的元素。关于如何在加载时对 DOM 中不存在的元素进行此操作的任何想法?

【问题讨论】:

    标签: jquery delegates


    【解决方案1】:

    变化:

    $(document).delegate("input.calendar", "click, focus", function() { 
        **myAppDatepicker**.datepicker("option", "maxDate", 0);
    });
    

    $(document).delegate("input.calendar", "click, focus", function() { 
        **$(this)**.datepicker("option", "maxDate", 0);
    });
    

    按预期附加附加选项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 2016-04-29
      • 2011-10-30
      相关资源
      最近更新 更多