jQuery和QUOT;本"在事件处理程序绑定问题(原型bindAsEventListener的当量) [英] jquery "this" binding issue on event handler (equivalent of bindAsEventListener in prototype)

查看:175
本文介绍了jQuery和QUOT;本"在事件处理程序绑定问题(原型bindAsEventListener的当量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在jQuery的事件哈德勒的绑定是事件生成的DOM元素(这点到的DOM元素)。
在原型改变的事件处理程序可以使用 bindAsEventListener 功能的结合;
如何从一个事件处理程序同时访问实例和DOM元素?结果
类似<一个href=\"http://stackoverflow.com/questions/117361/how-can-i-bind-an-event-handler-to-an-instance-in-jquery\">http://stackoverflow.com/questions/117361/how-can-i-bind-an-event-handler-to-an-instance-in-jquery

 功能车(){
    this.km = 0;
    $(#冲刺)点击(this.drive); //设置事件处理程序
}//事件处理程序
//它,我需要同时访问点击的元素
//并且绑定对象(汽车的实例)
Car.prototype.drive =功能(){
    this.km + = 10; //我想访问绑定(JQ,但它的变化)
    this.css({//也元素
        左:this.km
    });
    //注意这个函数里面我想没有访问它们在别处
}VAR车=新车();


解决方案

嗯,也许你可以使用jQuery.proxy()?

http://api.jquery.com/jQuery.proxy/

In jquery an event hadler's binding is the event generating DOM element (this points to the dom element). In prototype to change the binding of an event handler one can use the bindAsEventListener function; How can I access both the instance and the DOM element from a event handler?
Similar to http://stackoverflow.com/questions/117361/how-can-i-bind-an-event-handler-to-an-instance-in-jquery

function Car(){
    this.km = 0;
    $("#sprint").click(this.drive); //setup event handler
}

// event handler
// in it I need to access both the clicked element
// and the binding object (instance of car)
Car.prototype.drive = function(){
    this.km += 10; // i'd like to access the binding (but jq changes it)
    this.css({ // also the element
        left: this.km 
    }); 
    // NOTE that is inside this function I want to access them not elsewhere
}

var car = new Car();

解决方案

Hmm, maybe you can use jQuery.proxy()?

http://api.jquery.com/jQuery.proxy/

这篇关于jQuery和QUOT;本&QUOT;在事件处理程序绑定问题(原型bindAsEventListener的当量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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