且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何创建一个jQuery函数(一个新的jQuery方法或插件)?

更新时间:2023-11-20 23:10:34

Docs

 (function($){
$ .fn.myfunction =函数(){
alert('hello world');
return this;
};
})(jQuery);

然后你做了

pre> $( '#my_div')myfunction的();


I know that in JavaScript the syntax is as follows:

function myfunction(param){
  //some code
}

Is there a way to declare a function in jQuery that can be added to an element? For example:

$('#my_div').myfunction()

From the Docs:

(function( $ ){
   $.fn.myfunction = function() {
      alert('hello world');
      return this;
   }; 
})( jQuery );

Then you do

$('#my_div').myfunction();