Я пытаюсь написать плагин jQuery, который предоставит дополнительные функции/методы объекту, который его вызывает. Все обучающие материалы, которые я читал в Интернете (были просмотрены в течение последних 2 часов), включают, самое большее, как добавлять параметры, но не дополнительные функции.
Вот что я хочу сделать:
//format div должен быть контейнером сообщений, вызывая плагин для этого div
$("#mydiv").messagePlugin();
$("#mydiv").messagePlugin().saySomething("hello");
или что-то в этом роде. Вот что это сводится к: я вызываю плагин, а затем вызываю функцию, связанную с этим плагином. Я не могу найти способ сделать это, и я видел, как многие плагины делают это раньше.
Вот что у меня до сих пор для плагина:
jQuery.fn.messagePlugin = function() {
return this.each(function(){
alert(this);
});
//i tried to do this, but it does not seem to work
jQuery.fn.messagePlugin.saySomething = function(message){
$(this).html(message);
}
};
Как я могу достичь чего-то подобного?
Спасибо!
Обновление 18 ноября 2013 года. Я изменил правильный ответ на вопрос Хари, следуя комментариям и рекомендациям.