简体   繁体   English

事件处理程序中的jQuery“this”范围

[英]jQuery 'this' scope in event handler

I am unable to access parent object with in the event handler of the object!我无法在对象的事件处理程序中访问父对象!

I am creating a widget:我正在创建一个小部件:

(function( $ ) {

  $.widget('cs.window',{

     _create:function(){
       var me = this;
       this.append('<div class="close">X</div>');
       var close = this.element.find('.close');
       close.bind('click',function(){
        alert($(this));// this is referring event handler here
                       //but how to access `me` object here
        me.element.hide();//I want to achieve this, about but its saying me is undefined
      });
     }
  });

}(jQuery));

How to use me object in the click event handler?如何在单击事件处理程序中使用me对象?

If you bind a function with this._on then the this inside the callback is the widget itself.如果您使用this._on绑定一个函数,那么回调中的this就是小部件本身。 Try:尝试:

this._on(close, {
    click: function(){ this.element.hide(); }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM