如何创建一个jQuery函数(一个新的jQuery方法或插件)? [英] How to create a jQuery function (a new jQuery method or plugin)?

查看:95
本文介绍了如何创建一个jQuery函数(一个新的jQuery方法或插件)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在JavaScript中,语法如下:

$ p $ function myfunction(param){
//一些代码
}

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

  $('#my_div')。myfunction()


解决方案

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();

这篇关于如何创建一个jQuery函数(一个新的jQuery方法或插件)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆