I have created a 'control' using jQuery and used jQuery.extend to assist in making it as OO as possible.

During the initialisation of my control I wire up various click events like so

jQuery('#available input', 
            this.controlDiv).bind('click', this, this.availableCategoryClick);

Notice that I am pasing 'this' as the data argument in the bind method. I do this so that I can get at data attached to the control instance rather from the element that fires the click event.

This works perfectly, however i suspect there is a better way

Having used Prototype in the past, I remember a bind syntax that allowed you to control what the value of 'this' was in the event.

What is the jQuery way?

推荐答案

你可以将jQuery.proxy()和匿名函数一起使用,只是"context"是第二个参数有点尴尬.

 $("#button").click($.proxy(function () {
     //use original 'this'
 },this));

Jquery相关问答推荐

ASP.NET Core 8 MVC:从jQuery发布控制器中的所有值为空

用 jQuery/JavaScript 检测数字或字母?

如何以十六进制获取元素的背景 colored颜色 代码?

如何使用 jQuery 禁用粘贴(Ctrl+V)?

使用 JSONP 时如何捕获 jQuery $.getJSON(或数据类型设置为jsonp的 $.ajax)错误?

jQuery.parseJSON 与 JSON.parse

javascript 正则表达式用于包含至少 8 个字符、1 个数字、1 个大写和 1 个小写的密码

ajax调用后jQuery点击功能不起作用?

$(document).on('click', '#id', function() {}) 与 $('#id').on('click', function(){})

使用 jQuery 拖放防止单击事件

如何使用 Twitter Bootstrap 自动关闭alert

更改 Eclipse 设置以忽略特定文件上的错误

如何在 JQuery 中 Select 除单击元素之外的所有类?

如何让 twitter bootstrap 子菜单在左侧打开?

使用 jQuery 比较两个 Javascript 对象数组

设置 JQuery event.preventDefault() 时绕过 window.open 上的弹出窗口阻止程序

jQuery位置href

为什么要定义一个匿名函数并将 jQuery 作为参数传递给它?

删除所有以某个字符串开头的类

jQuery .each() 索引?