好的,所以我无法获得如何在jquery插件之间共享设置,无论我做什么,我的设置在循环槽元素时都会被重写。这是我的代码:
(function ( $ ) {
var default_settings = {
auto_start: false,
tools: {}
};
function test(settings){
//here is the problem it gets wrong element
settings.tools.progress_bar.animate({
width: "100%"
});
}
var methods = {
init: function(element, options){
var settings = $.extend({}, default_settings, options);
settings.tools.progress_bar = $('<div class="test"></div>');
$(element).append(
settings.tools.progress_bar
);
if(settings.auto_start)
test(settings);
$.data(element, "settings", settings);
},
start: function(){
var settings = $.data(this, "settings");
test(settings);
}
}
$.fn.ajaxupload = function(method, options){
return this.each(function(){
if(methods[method])
return methods[method].apply(this, [options]);
else if(typeof method === 'object' || !method)
return methods.init(this, method);
});
}
})( jQuery);我需要能够循环使用多个元素,然后调用公共函数来启动一些操作或其他一些事情,比如:
$(".container").ajaxupload();
$(".container2").ajaxupload();
//even though I call for container it will run animation for container2
$(".container").ajaxupload("start");我也做了jsfiddle,但至少对我来说,目前它几乎不起作用。
发布于 2012-11-27 15:16:53
Simpy替换
var settings = $.extend({}, default_settings, options);使用
var settings = $.extend(true, {}, default_settings, options);演示:http://jsfiddle.net/jupSp/12/
如果没有深度复制(第一个参数为真),它只复制第一级对象,在您的示例中,它导致settings.tools指向default_settings.tools并调用settings.tools.progress_bar = $('<div class="test"></div>');,覆盖default_settings.tools.progress_bar,最后一个progress_bar附加到所有元素。
https://stackoverflow.com/questions/13586611
复制相似问题