Javascript 在jquery样板文件中的何处实现window.resize函数?
我正在尝试将使用jquery插件模式编写的一个插件转换为由提供的插件。我的插件依赖于Javascript 在jquery样板文件中的何处实现window.resize函数?,javascript,jquery,plugins,jquery-plugins,jquery-boilerplate,Javascript,Jquery,Plugins,Jquery Plugins,Jquery Boilerplate,我正在尝试将使用jquery插件模式编写的一个插件转换为由提供的插件。我的插件依赖于$(window).resize()函数使其响应,但是当我尝试在jquery样板文件上使用它时,当我调整窗口浏览器的大小时,web控制台返回一个类型错误: function Plugin ( element, options ) { this.element = element; this.cfg = $.extend( true, defaults, options ); this._d
$(window).resize()
函数使其响应,但是当我尝试在jquery样板文件上使用它时,当我调整窗口浏览器的大小时,web控制台返回一个类型错误:
function Plugin ( element, options ) {
this.element = element;
this.cfg = $.extend( true, defaults, options );
this._defauls = defaults;
this._name = pluginName;
this.init();
}
// Avoid Plugin.prototype conflicts
$.extend( Plugin.prototype, {
init: function() {
this.windowWidth();
$(window).resize(function(){
this.windowWidth();
});
},
windowWidth: function() {
var w = $( window ).width();
console.log(w);
}
} );
Web控制台返回:
TypeError:this.windowWidth不是一个函数
我也这样试过:
function Plugin ( element, options ) {
this.element = element;
this.cfg = $.extend( true, defaults, options );
this._defaults = defaults;
this._name = pluginName;
this.init();
$(window).resize(function(){
this.init();
});
}
// Avoid Plugin.prototype conflicts
$.extend( Plugin.prototype, {
init: function() {
this.windowWidth();
},
windowWidth: function() {
var w = $( window ).width();
console.log(w);
}
} );
web控制台将返回:
TypeError:this.init不是函数
我必须把那些必须听jquery resize方法的代码放在哪里
我基本上是这样做的:
function Plugin ( element, options ) {
var element = $( element ),
cfg = $.extend( true, defaults, options ),
windowWidth = function() {
return $( window ).width();
};
console.log( windowWidth() );
$(window).resize(function(){
console.log( windowWidth() );
});
}
但是这不是jquery样板团队的目的,所以在使用jquery样板插件模式时如何做到这一点?此错误与jquery样板无关。 在编写任何插件之前,您肯定应该在Javascript中了解更多关于“this”关键字的信息。