Javascript 需要一些帮助来编写自定义jQuery插件吗
请看以下内容:Javascript 需要一些帮助来编写自定义jQuery插件吗,javascript,jquery,jquery-plugins,Javascript,Jquery,Jquery Plugins,请看以下内容: jQuery.fn.jqPos = function(target, settings) { settings = jQuery.extend({ offset: [ 0, 0 ] }, settings); return this.each(function() { magic($(this), target, settings); $(window).resize(function(){
jQuery.fn.jqPos = function(target, settings) {
settings = jQuery.extend({
offset: [ 0, 0 ]
}, settings);
return this.each(function() {
magic($(this), target, settings);
$(window).resize(function(){
magic($(this), target, settings);
});
});
function magic(self, target, settings) {
// Here I position self close to target
}
};
这在我第一次初始化插件时非常有效,比如$('div#one').jqPos($('div#two')
和magic方法按其应该的方式运行。但是在eventwindow.resize
中,什么也没有发生(我希望它使用相同的设置和参数运行相同的方法)
为什么?如何克服
EDIT:在magic方法(在window.resize)中,参数都是
未定义的您混淆了$(window.resize)中此所指的内容。resize(function(){
魔术($(此)、目标、设置);
});
此
不再指您的元素,而是指窗口本身。尝试:
return this.each(function() {
var $this = $(this);
magic($this, target, settings);
$(window).resize(function(){
magic($this, target, settings);
});
});