Я создаю один простой пользовательский плагин jQuery, используя jquery.boilerplate.js. Теперь я хочу, чтобы создать одну функцию, которая будет вызывать как,Как создать функцию для пользовательского плагина jQuery
var div1 = $("#div1").changeBackgroundColor({
color: $('#colorCode').val().trim()
});
div1.getColor();
Как определить, что getColor()
метод JQuery плагин.
Пользовательские Plugin:
;(function($, window, document, undefined) {
var pluginName = "changeBackgroundColor", defaults = {
color : "black"
};
function Plugin(element, options) {
this.element = element;
this.settings = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
$.extend(Plugin.prototype, {
init : function() {
console.log("Hello");
}
});
$.fn[pluginName] = function(options) {
this.each(function() {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName, new Plugin(this,
options));
}
console.log(options);
if(options===undefined){
$(this).css("background-color", defaults.color);
} else{
$(this).css("background-color", options.color);
}
});
return this;
};
})(jQuery, window, document);
Спасибо .... :)
@ Юрий. Вы должны написать блог на эту тему. Благодаря.....:) – Sheel