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。在我用这个之前它起作用了。只是把numpanel作为一个普通变量,但现在它没有了。很抱歉,我不能提供更多的信息,但我的问题是:如何使jQuery插件中的变量“可编辑” 编辑 我为任何混乱道歉-我只是不想发布大量愚蠢的代码,使其无法阅读。请看我上面的完整代码jQuery插件变量,jquery,variables,jquery-plugins,scope,Jquery,Variables,Jquery Plugins,Scope,我有以下代码 (function($) { // carousel var Carousel = { settings: { itemsPerPage: 1, itemsPerTransition: 1, noOfRows: 1, pagination: true, nextPrevLinks: true, speed:
编辑2我想吸一个枕头。。。看看autoSwitch函数-这就是我遇到错误的地方:panelWidth未定义,这就是我尝试使用此函数的原因。听起来好像在使用全局变量。这不是好的做法。这可能需要包装在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.
听起来好像你在使用一个全局变量。这不是好的做法。这可能需要包装在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.
这个.numPanels++在each中调用的匿名函数的作用域中是本地的。如果将初始声明替换为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
});
这个.numPanels++在each中调用的匿名函数的作用域中是本地的。如果将初始声明替换为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
});
这个范围的问题 您在everithing之外创建了this变量,然后希望在each函数内部调用this变量 它们有不同的范围 您需要在每个对象中创建一个可访问的全局变量 var numPanels=0
然后你就可以使用numPanels++ 这个范围的问题 您在everithing之外创建了this变量,然后希望在each函数内部调用this变量 它们有不同的范围 您需要在每个对象中创建一个可访问的全局变量 var numPanels=0
然后你就可以使用numPanels++ 我为混淆道歉-请参阅我的OP了解全部代码。我应该说得更清楚些。我为这一混乱道歉-请参阅我的OP了解全部代码。我应该说得更清楚些。我为这一混乱道歉-请参阅我的OP了解全部代码。我应该说得更清楚些。我为这一混乱道歉-请参阅我的OP了解全部代码。我应该说得更清楚。编辑我的作品请看编辑2位-抱歉。编辑我的作品请看编辑2位-抱歉。