如何创建一个 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();