Javascript 在Jquery模式中定义此范围;“轻量化”;
我希望你能理解我的英语 我尝试使用Jquery模式“Lighrweight”() 但是我对调用方法和它的范围有一个问题 我在表上绑定插件:Javascript 在Jquery模式中定义此范围;“轻量化”;,javascript,design-patterns,scope,Javascript,Design Patterns,Scope,我希望你能理解我的英语 我尝试使用Jquery模式“Lighrweight”() 但是我对调用方法和它的范围有一个问题 我在表上绑定插件: $("#tableSurface0").flexTab(); $("#tableSurface1").flexTab(); 我的Jquery插件: ;(function ( $, window, document, undefined ) { var pluginName = 'flexTab', default
$("#tableSurface0").flexTab();
$("#tableSurface1").flexTab();
我的Jquery插件:
;(function ( $, window, document, undefined ) {
var pluginName = 'flexTab',
defaults = {
wrapOverflowHeight : true
};
// The actual plugin constructor
function Plugin( element, options ) {
this.element = element;
this.options = $.extend( {}, defaults, options) ;
this.init();
}
Plugin.prototype.init = function () {
$("th:not([noflex])", this.element).on("mousedown", menuContextuel);
};
menuContextuel = function(event)
{
//BUG
console.log( ?? ); // show this.element of constructor
}
// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[pluginName] = function ( options ) {
return this.each(function () {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName,
new Plugin( this, options ));
}
});
}
})( jQuery, window, document );
因此,如果不在事件处理程序中添加数据,我无法在MenuContextel函数中调用this.element:
Plugin.prototype.init = function () {
$("th:not([noflex])", this.element).on("mousedown", { table:this.element }, menuContextuel);
};
...
menuContextuel = function(event)
{
console.log( event.data.table );
}
他还有别的办法吗
谢谢这是因为函数正在被调用,“this”是单击的元素。使用可以使用,以便维护您所关注的“this”的范围
$("th:not([noflex])", this.element).on("mousedown", $.proxy(menuContextuel,this));
谢谢你,我不知道$.proxy。最后一个问题。如果在我的ContextureMenu函数中,我希望在我的元素中单击enter,那么最好的方法是它$(th:not([noflex]),this.element)。。。menucontextel=function(event){console.log(this.element);//great console.log(event.currentTarget)//单击的}