How to make custom function for jQuery selector

2019-04-24 12:24发布

问题:

How does one go about making a custom function to append to the jQuery selector? Something that would look like:

$('.my_class').my_function();

回答1:

You want to add the function to jQuery.fn (which is a reference to the jQuery object prototype). For more details check out:

http://docs.jquery.com/Plugins/Authoring

An example\stub:

(function($){
  $.fn.my_function = function(){ ... };

})(jQuery);