jQuery的委托参数顺序 [英] jQuery's Delegate Parameter Order

查看:113
本文介绍了jQuery的委托参数顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery的委托方法的签名记录如下:

The signature for jQuery's delegate method is documented as such:

.delegate( selector, eventType, handler )

是否有任何情况会使参数按照以下顺序排序,交换eventType和选择器?

Are there any circumstances that would make the parameter order the following, swapping eventType and selector?

.delegate( eventType, selector, handler )

我有一个项目,无论做什么,我都必须将eventType放在首位.例如,这是我当前项目中唯一起作用的东西(这只是测试,而不是实际代码!):

I have a project that no matter what I do, I have to put the eventType first. For example this is the only thing that works in my current project (this is just a test, not real code!):

$('body').delegate('click', 'input', function (e) {
            alert("Why is this the only way it will work for me in this project?");
        });

根据文档,这应该可以工作:

According to the documentation, this should work:

$('body').delegate('input', 'click', function (e) {
            alert("This will never get hit");
        });

我在小型项目中进行了测试,其工作方式就像文档中所说的那样(这是一个js小提琴示例: http://jsfiddle.net/wmdYr/16/)

I tested with small projects and it works like the documentation says it should (here's an example js fiddle: http://jsfiddle.net/wmdYr/16/)

我正在使用Google CDN的jQuery 1.7.1,并尝试了一些具有相同结果的较旧版本.

I'm using jQuery 1.7.1 from the Google CDN, and tried a few older versions with the same results.

在某些情况下,是否有必要将Event作为第一个参数?

Is there a reason that the Event needs to be the first parameter in some circumstances?

推荐答案

我已解决了该问题,现在可以将已记录的参数顺序用于jQuery的委托方法:.delegate( selector, eventType, handler(eventObject))

I resolved the issue and can now use the documented order of parameters for jQuery's delegate method: .delegate( selector, eventType, handler(eventObject))

我们的解决方案是将jQuery Validate库升级到最新版本(1.9).我正在处理的项目使用了Asp.Net MVC附带的旧版本的jQuery验证库(1.5.5).该版本似乎与jQuery的委托方法冲突.

The solution for us was to upgrade the jQuery Validate library to the latest version (1.9). The project I am working on used an older version of the jQuery validate library (1.5.5) that shipped with Asp.Net MVC. That version appears to have conflicts with jQuery's delegate method.

由于Backbone.js在后台使用jQuery的委托方法,因此该冲突影响jQuery的委托方法和Backbone.js视图事件.

This conflict affects jQuery's delegate method and Backbone.js view events since Backbone.js uses jQuery's delegate method under the hood.

这篇关于jQuery的委托参数顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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