我想创建两个 JQuery 函数。其中一个将使用在另一个函数中创建的数据。说:
jQuery.fn.myScroller = function (child) {
var $moveable = this.children(child);
var $children = this.children(child).children();
posl = posl.replace('px', '');
post = post.replace('px', '');
var varList = [];
$children.each(function () {
var currVar = {};
currVar.w = $(this).width();
varList.push(currVar);
});
// I want to save varList variable somehow.
this.varList = varList; // (1)
};
jQuery.fn.scrollUnitLeft = function () {
// Now I need to use this varList if this function called
// But the following returns undefined.
// However, I've saved it inside this at (1)
console.log(this.varList)
// More code...
}
$('#main_div').myScroller("#dummydiv");
$('#main_div').scrollUnitLeft();
正如我在代码中的注释中解释的那样,这不起作用。
我怎样才能做到这一点?