在jQuery事件中控制'this'的值 [英] Controlling the value of 'this' in a jQuery event

查看:79
本文介绍了在jQuery事件中控制'this'的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用jQuery创建了一个控件",并使用jQuery.extend来帮助使其尽可能地面向对象.

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);

请注意,我正在将"this"作为bind方法中的数据参数.我这样做是为了获得附加到控件实例的数据,而不是从激发click事件的元素中获取数据.

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

我过去曾经使用过Prototype,但我记得绑定语法允许您控制事件中'this'的值.

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.

什么是jQuery方式?

What is the jQuery way?

推荐答案

您可以将 jQuery.proxy() 与匿名用户一起使用函数,第二个参数只是尴尬".

You can use jQuery.proxy() with anonymous function, just a little awkward that 'context' is the second parameter.

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

这篇关于在jQuery事件中控制'this'的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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