5
例如,我正在查看jCalendar源代码,创建者有两个不同的插件部分,一个是“jQuery.jcalendar”下的一个函数,另一个是“jQuery.fn.jcalendar”。将两者分开的目的是什么?一个人做了什么?jQuery插件创作:为什么有些人做jQuery.pluginName和其他人jQuery.fn.pluginName?
例如,我正在查看jCalendar源代码,创建者有两个不同的插件部分,一个是“jQuery.jcalendar”下的一个函数,另一个是“jQuery.fn.jcalendar”。将两者分开的目的是什么?一个人做了什么?jQuery插件创作:为什么有些人做jQuery.pluginName和其他人jQuery.fn.pluginName?
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对象本身其实是做ne用于你的类需要但不扩展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