2009-02-11 50 views

回答

10

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