JQuery将事件附加到实例中的方法
我的代码看起来像这样….
function Finder(id) { this.id = id; this.input = $("#" + this.id + " :input[type='text']:first")[0]; $(this.input).bind('keyup'....); this.KeyUpHandler = function (e) { ..the event should trigger this.. }
this.input =在’id’中找到的类型输入的第一个元素,这是我将要引用的内容。 这适用于我需要的东西。
我想要做的是在’input’上绑定keyup事件。 但是我希望事件引用我的函数中包含的实例方法 – this.KeyUpHandler()。
另外,我需要’e’才能成为传入函数的事件,我刚刚在输入的标记上做了这个(onkeypress =“keyuphandler();”)。
任何想法我如何将事件绑定到我正在工作的函数实例中的函数?
function Finder(id) { this.id = id; this.input = $("#" + this.id + " :input[type='text']:first")[0]; that=this; this.KeyUpHandler = function (e) { ..the event should trigger this.. } $(this.input).bind('keyup', this.KeyUpHandler); }
定义函数后调用bind()
非常重要!
this.KeyUpHandler = function (e) { ..the event should trigger this.. } $(this.input).bind('keyup', function(){ this.KeyUpHandler(event); //more code can go here });