My Plugin dont overwrite the default values
I try to realise my first jquery plugin (Im a noob).
(function($){
$.test = function开发者_运维技巧(selector, settings){
// settings
var config = {
'text': "test"
};
if ( settings ){$.extend(config, settings);}
// variables
var i = 0;
alert(config.text);
};
})(jQuery);
Calling
$.test("juhu");
The script should alert "Juhu" not the default value "test".
What is the fault?Could somebody help me please?
Try to extend this way:
if (settings) { config = $.extend( {}, config, settings ); }
Then access the value with config.text
You'll need to assign the return value of $.extend
. Try something like
function(selector, settings){
var config = {
'text': "test"
};
config = $.extend(config, settings || {});
}
you are calling
$.test("juhu");
but your function takes two parameters
$.test = function(selector, settings)
so "juhu" becomes the selector, settings is undefined which means if (settings) fails and the default "test" string is alerted.
精彩评论