2011-03-09 124 views
1

我有以下代码jQuery插件变量

 
(function($) { 
    // carousel 
    var Carousel = { 
     settings: { 
      itemsPerPage: 1, 
      itemsPerTransition: 1, 
      noOfRows: 1, 
      pagination: true, 
      nextPrevLinks: true, 
      speed: 'normal', 
      easing: 'swing', 
      loop: false, 
      auto: true, 
      autoTime: 4000, 

      maxHeight: 300, 
      maxWidth: 500 
     }, 
     init: function(el, options) 
     { 
      if (!el.length) 
      { 
       return false; 
      } 

      this.options = $.extend({}, this.settings, options); 

      this.container = el; 

      this.panelHeight = 0; 
      this.panelWidth = 0; 
      this.numPanels = 0; 

      // Find biggest panel in set 
      this.container.find(".tinycarousel > li > div").each(function() 
      { 
       if($(this).outerHeight() > this.panelHeight) 
       { 
        this.panelHeight = $(this).outerHeight(); 
       } 

       if($(this).outerWidth() > this.panelWidth) 
       { 
        this.panelWidth = $(this).outerWidth(); 
       } 

       this.numPanels++; 
      }); 

      alert(this.numPanels); 
     }, 
     autoSwitch: function() 
     { 
      this.container.find(".tinycarousel").animate({marginLeft: -panelWidth}); 
     } 
    }; 

    // bridge 
    $.fn.tinycarousel = function(options) { 
     return this.each(function() { 
      var obj = Object.create(Carousel); 
      obj.init($(this), options); 
      $.data(this, 'carousel', obj); 
     }); 
    }; 
})(jQuery); 

我在这里的问题是,this.numPanels给出0的结果(和我知道应该是3)。它在我使用this.之前有效,并且只有numPanels作为一个普通变量,但现在它不是。对不起,我无法提供更多信息,但我的问题是:我如何在jQuery插件'可编辑'内部创建变量。

编辑 我对任何混淆道歉 - 我只是不想发布愚蠢的代码,并使其不可读。请看我上面的完整代码。

EDIT 2我感觉像吸了pillock ......看看在autoSwitch功能 - 这是我得到一个错误:panelWidth is not defined这就是为什么我试图使用this.

回答

9

this.numPanels ++是在范围上本地您的每()调用的匿名函数。如果您将初始声明替换为var numPanels = 0;并在你的函数中访问它(或通过引用在numPanels中传递),你会得到预期的结果。

做这样的:

this.panelHeight = 0; 
this.panelWidth = 0; 
this.numPanels = 0; 
var self = this; // reference to this 
// Find biggest panel in set 
this.container.find(".tinycarousel > li > div").each(function() { 
    if($(this).outerHeight() > self.panelHeight) { // use self 
     self.panelHeight = $(this).outerHeight(); // use self 
    } 

    if($(this).outerWidth() > self.panelWidth){ // use self 
     self.panelWidth = $(this).outerWidth(); // use self 
    } 
    self.numPanels++; // use self 
}); 
+0

我对此感到抱歉 - 请参阅我的OP了解整个代码。我应该已经更清楚了。 – Bojangles 2011-03-09 20:46:36

+0

更多编辑:-(对不起! – Bojangles 2011-03-09 20:52:19

+0

@JamWaffles - 查看编辑 – 2011-03-09 21:08:54

2

这听起来像你在工作时使用全局变量。这不是很好的做法。 this可能需要包装在jquery函数调用中。先试着做$(this).numPanels。我看不到剩下的代码,所以我不知道这是否可行。如果不存在,则尝试通过将其附加到DOM元素来存储该变量。像这样简单的例子:

$('#myElement').data('numPanels', 0); // Storing the data. 

var numPanels = $('#myElement').data('numPanels'); // Retrieving the data. 
numPanels++; // Do work. 
$('#myElement').data('numPanels', numPanels); // Storing the new data. 
+0

我的混乱道歉 - 请我OP整个代码。我应该更清楚。 – Bojangles 2011-03-09 20:46:20

+0

更多编辑:-(对不起! – Bojangles 2011-03-09 20:53:11

0

问题与此范围内。

您在永久性之外创建了一个此变量,然后您想在每个函数中调用此变量。

他们有不同的范围。

您需要创建一个全局变量,以便在每个对象中都可访问。

var numPanels = 0;

,然后哟可以使用numPanels++;

+0

编辑我的OP(见**编辑2 **位) - 对不起。 – Bojangles 2011-03-09 20:53:30