2011-02-26 71 views
2

我想创建我的第一个jQuery插件,并且我正在创建this.each is not a function错误,它在bindUi方法中引用此行return this.each(function() {。我认为this指的是“插件被调用的jQuery对象”,在我的情况下,它将是我在使用$('#location').locationSearch()创建插件时使用的#location节点,对不对?页面加载时出现此错误。jQuery插件'this.each不是函数'

这里是我的插件代码:

(function($) { 
    var methods = { 
    init : function(options) { 

     return this.each(function() { 

     var settings = { 
      //none yet 
     }; 

     if (options) { 
      $.extend(settings, options); 
     }   
     methods.log('init'); 

     //attach events 
     methods.bindUi();   
     }); 

    }, 

    destroy : function() { 
     return this.each(function(){ }); 
    }, 

    bindUi : function() { 

     return this.each(function() { 

     methods.log('bindUi'); 
     this.click(function() { 
      methods.log('clicked'); 
     }); 

     }); 

    },  

    log : function(text) { 
     if (window.console && window.console.log) { 
     window.console.log(text); 
     }; 
    } 
} // end methods 

    jQuery.fn.locationSearch = function(method) { 
    if (methods[method]) { 
     return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); 
    } else if (typeof method === 'object' || ! method) { 
     return methods.init.apply(this, arguments); 
    } else { 
     $.error('Method ' + method + ' does not exist on jQuery.tooltip'); 
    }  
    }; 
})(jQuery); 

我也是,如果我的方法,每个人都需要,如果仅init和destroy方法,用它来使用return this.each(function() { ..还是有点糊涂?

回答

4

你必须调用

methods.bundUi.apply(this); 

init方法。

如果你只做methods.bindUi(),那么this里面的bindUi就是指methods这个对象。

更新:

哦,我刚看到它。您正在拨打methods.bindUi()里面的each循环。然后this将参考当前的DOM元素。所以现在的问题是,你想在bindUi内提及什么?所有选中的元素,还是只有循环中的当前元素?

下面是两种possibilites:

A:所有的元件

init : function(options) { 
    this.each(function() { 
     var settings = { 
      //none yet 
     }; 
     if (options) { 
      $.extend(settings, options); 
     }   
     methods.log('init'); // <- this might be also better outside the loop 
    }); 
    //attach events 
    methods.bindUi.apply(this); // <- called outside the each loop 

    return this; 
} 

(无需改变bindUi

B:一个元件this里面的each循环中的init将参考当前的DOM元素。我们当时只处理一个元素,所以我们不需要在bindUi方法中使用each。 (仍然使用methods.bindUi.apply(this)init(但在循环内部,就像你已经拥有它一样))。但您不能再从外面拨打bindUi(即您不能拨打.locationSearch('bindUi'))。

bindUi : function() { 
    // don't need `each`, as `this` will refer to only one element. 
    methods.log('bindUi'); 
    $(this).click(function() { 
      methods.log('clicked'); 
    }); 
}, 

第三个解决方案确实是只改变this$(this)bindUi函数内。这肯定会工作,但有两个“误解”:

  • 如果该功能是通过.locationSearch('bindUi')称为然后this将已经是一个jQuery对象并调用$(this)是多余的。
  • 如果调用从each环的功能init,然后this将参考一个 DOM元素。所以调用$(this)是必要的,但使用each来循环过一个元素是毫无意义的。

我也有点糊涂了,如果我的方法,每个人都需要使用return this.each(function() { ..或者如果只有initdestroy方法使用它?

应该可以通过.locationSearch(method)调用的每种方法应该是return this

+0

我用'methods.bindUi.apply(this);'替换'methods.bindUi();'时仍然出现同样的错误。 – imns

+0

@bababa:请参阅我的更新。 –

+0

修复它,现在一切正常完美......我正在写一些代码。谢谢! – imns

2

尝试$(this).each(

jQuery通常允许您使用this来引用纯HTML元素(而不是jQuery包装的元素)。你必须重新包装才能获得jQuery的功能。

+0

不在插件中。 –

+0

因此,如果我使用'methods.bindUi.apply(this);'和'$(this)',它现在可以工作。 – imns

+0

@Felix,它看起来像文档说不要在插件中使用'$(this)',但是如果没有它,我不能得到它。有什么建议么? – imns

相关问题