Имя jQuery.fn.mypluging расширяет объекты jQuery:
$(selector); //a jquery object
$(selector).myplugin();
jQuery.myplugin расширяет сам объект jquery:
$; //the jQuery object
$.myPlugin();
Добавив свой плагин в jQuery.fn, вы можете делать вещи с объектами, найденными этим селектором:
jQuery.fn.makeRed = function(){
this.each( function() {
$(this).css('color', 'red');
}
}
$('div.someClass').makeRed(); //makes all divs of class someclass have red text
Расширение самого объекта jQuery обычно делается для функций, которые нужны вашему классу, но которые не расширяют объекты jQuery. Итак, чтобы расширить наш предыдущий пример:
jQuery.fn.doStuff = function(){
this.each( function() {
$(this).css('color', 'red')
.append($.doStuff.giveMeRandom());
}
}
jQuery.doStuff = {
giveMeRandom: function() {
return Math.random();
}
}
$('div.someClass').doStuff(); //makes all divs of class someclass have red text and append a random number to them