jQuery插件。将其设置在输入端。
onkeydown
事件必须调用KeyDown
. 但是响应控制台,我看到了:
Uncaught TypeError: Object #<HTMLInputElement> has no method 'KeyDown'
在编写插件时,我是新手。
;(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 );