2013-07-12 114 views
0

jQuery插件。将其设置在输入上。JQuery插件不起作用

onkeydown事件必须调用KeyDown。但对于控制台的回应,我看到:

Uncaught TypeError: Object #<HTMLInputElement> has no method 'KeyDown' 

在写插件我是一个新手。因为它是由jQuery的覆盖

;(function ($, window, document, undefined) { 

function Plugin(element, options) { 

    var defaults = { 
     width:  270, 
     height:  300 
    }; 

    this.element = element; 
    this.el   = $(element); 
    this.options = $.extend({}, defaults, options) ; 

    this.init(); 
} 

Plugin.prototype = { 

    init: function() { 
     this.el.on('keydown', function (e) { this.KeyDown(e); }); 
    }, 

    KeyDown: function (e) { 
     console.log('Yeah!!!'); 
    } 

}; 

$.fn.myplugin= function (options) { 
    return this.each(function() { 
     if (!$.data(this, 'plugin_myplugin')) { 
      $.data(this, 'plugin_myplugin', 
      new Plugin(this, options)); 
     } 
    });  
} 
})(jQuery, window, document); 
+0

@soul,你甚至看这个问题? –

+0

你有更多的代码可以看吗? HTML,以及您在哪里启动/包含插件。 – putvande

回答

3

this关键字不会在功能function (e) { this.KeyDown(e); }内工作。新的this引用该元素。

尝试类似:

init: function() { 
    var self = this; 
    this.el.on('keydown', function (e) { self.KeyDown(e); }); 
},