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方法按其应该的方式运行。但是在event
window.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);
        });
    });