2013-04-14 30 views
0

的jsfiddle的方法:http://jsfiddle.net/bg9vL/

控制檯輸出:試圖寫一個jQuery插件,但我不知道該怎麼稱呼由init

Uncaught TypeError: Object [object global] has no method 'each'

var methods = { 

    init: function (options) { 

     // Create some defaults, extending them with any options that were provided 
     var settings = $.extend({ 
      'maxWidth': 0, 
      'maxHeight': 0, 
      'forceAspect': true 
     }, options); 

     return this.each(function() { 

      // if max size wasn't specified, position only 
      var nosize = settings.maxWidth === 0 || settings.maxHeight === 0; 
      var container = $(this).parent(); 

      $(this).css('position', 'relative'); 

      // fit when window is resized 
      $(window).bind('resize.tailorfit', methods.fit); 

      // fit when page has loaded 
      $(window).bind('load.tailorfit', methods.fit); 

      // fit on initial load 
      methods.fit.apply(); 
     }); 

    }, ... 
+0

回調是的!它在js部分。 – rtheunissen

+0

哎呀,對不起。但是我沒有收到你指定的錯誤。在imgur圖像上獲得403。 – Dogbert

+0

這很奇怪......這是一個很好的鏈接。 – rtheunissen

回答

1

我敢打賭,你需要用這與一個jQuery標記。

... 
$(this).each(
... 

它不會將其識別爲jQuery對象,因此每個函數都不可用。

你確實有容器問題,你應該能夠輕鬆解決。

+0

不,傳遞給插件函數的對象是一個jQuery對象,而不是DOM元素/數組。 – Dogbert

+0

它確實通過了錯誤。 o.O – rtheunissen

+0

當我在他的jFiddle中做了一個$(this)時,那個錯誤消失了。 –

1
methods.fit.apply(); 

與全局對象,因爲這,它沒有方法調用fiteach

現在,你可以在技術上通過在功能fit$(this)解決這個問題,但我不知道這是你想做。

也注意到,您的jsfiddle不執行給$('.image-container > img').load

相關問題