如何将bind()pass-through(to on())转换为delegate()-就像jQuery 1.7.1代码

How to convert bind() pass through (to on()) to delegate()-like right inside jQuery 1.7.1 code?

本文关键字:就像 delegate jQuery 1代码 on bind pass-through to 转换      更新时间:2023-09-26

我正在尝试让jQuery的load()更新一些Drupal 7内容,但相关的JS代码没有对其进行处理。有问题的代码使用bind(),分布在数十个Drupal核心JS文件中。

我想通过使用jQuery 1.7.1并更改来解决这个问题

bind: function( types, data, fn ) {
    return this.on( types, null, data, fn );
},

表现得像

delegate: function( selector, types, data, fn ) {
    return this.on( types, selector, data, fn );
},

正如您所看到的,唯一缺少的是selector。我能从标准的bind()调用中获得它吗?

您可以尝试类似的东西

function(types, data, fn) {
    (this.context
      ? $(this.context)
      : this ).on(types, this.selector || null, data, fn);
    return this;
}

因为每个jQuery对象都将当前选择器和上下文元素作为属性。

但是,您不应该通过用delegate功能性覆盖.bind来应用解决方法,这只会在您的应用程序中引入错误。最好更改调用绑定但不应该调用的代码。

相关文章: