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

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

问题描述

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

I know that in JavaScript the syntax is as follows:

function myfunction(param){
  //some code
}

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

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

$('#my_div').myfunction()

推荐答案

来自 Docs:

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

然后你做

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

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

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