小编典典

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

javascript

我知道JavaScript中的语法如下:

function myfunction(param){
  //some code
}

有没有一种方法可以在jQuery中声明可以添加到元素的函数?例如:

$('#my_div').myfunction()

阅读 482

收藏
2020-05-01

共1个答案

小编典典

文档中

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

那你做

$('#my_div').myfunction();
2020-05-01