我正在玩一个非常简单的jQuery插件"hello world“,但是我在使用jQuery Plugin Boilerplate时遇到了一些麻烦。例如,即使简单明了并且注释良好,我也觉得我漏掉了一点,无论我使用什么jQuery方法,我都会得到错误。下面是我的init函数。
Plugin.prototype = {
init: function() {
console.log(this, this.element); // this.element return my element works fine
this.element.on('click', function() { // 'undefined' is not a function :(
alert('whoa');
});
this.element.click(function() { // not working at all :'(
alert('whoa');
});
},
open: function(el, options) {
// some logic
}
};
你能给我一个提示吗?
发布于 2012-12-18 22:27:07
由于您正在使用一个框架来制作jQuery插件,因此您必须遵循它们的约定。
如果您只是在执行$.fn.plugin = function(){}
,那么this
将是一个jQuery对象,因为$.fn
是对jQuery.prototype
的引用。
在您的框架中,this.element
是原生DOM元素,因此要使用jQuery方法,需要将其包装在$()
中。
$(this.element).on('click', function(){
});
https://stackoverflow.com/questions/13923179
复制相似问题