如何向jQuery添加一个函数?

时间:2022-01-08 16:55:43

What's the easiest way to define a new jQuery member function?

定义新的jQuery成员函数最简单的方法是什么?

So that I can call something like:

所以我可以打电话给:

$('#id').applyMyOwnFunc()

6 个解决方案

#1


90  

Please see Defining your own functions in jQuery:

请参阅在jQuery中定义自己的函数:

In this post i want to present how easy define your own functions in jQuery and using them.

在这篇文章中,我想介绍如何在jQuery中轻松定义自己的函数并使用它们。

From the post:

从帖子:

jQuery.fn.yourfunctionname = function() {
    var o = $(this[0]) // It's your element

    return this; // This is needed so others can keep chaining off of this
};

Used:

用过的:

$(element).yourfunctionname()

#2


30  

This is the pattern that I prefer to define my own plugins.

这是我更喜欢定义自己的插件的模式。

(function($) {

    $.fn.extend({
        myfunc: function(options) {
            options = $.extend( {}, $.MyFunc.defaults, options );

            this.each(function() {
                new $.MyFunc(this,options);
            });
            return this;
        }
    });

    // ctl is the element, options is the set of defaults + user options
    $.MyFunc = function( ctl, options ) {
         ...your function.
    };

    // option defaults
    $.MyFunc.defaults = {
        ...hash of default settings...
    };

})(jQuery);

Applied as:

应用为:

$('selector').myfunc( { option: value } );

#3


17  

The jquery documentation has a section on plugin authoring, where I found this example:

jquery文档有一个关于插件创作的部分,我在这里找到了这个例子:

jQuery.fn.debug = function() {
  return this.each(function(){
    alert(this);
  });
};

Then you'd be able to call it this way:

然后你就可以这样称呼它:

$("div p").debug();

#4


11  

jQuery has the extend function to do that

jQuery有扩展功能来做到这一点

jQuery.fn.extend({
  check: function() {
    return this.each(function() { this.checked = true; });
  },
  uncheck: function() {
    return this.each(function() { this.checked = false; });
  }
});

you can see the documentation there

你可以在那里看到文档

#5


3  

Here's a plugin, in it's simplest form...

这是一个插件,最简单的形式......

jQuery.fn.myPlugin = function() {
  // do something here
};

You'll really want to consult the documentation though:

您真的想查阅文档:

http://docs.jquery.com/Plugins/Authoring

http://docs.jquery.com/Plugins/Authoring

#6


0  

/* This prototype example allows you to remove array from array */

Array.prototype.remove = function(x) {
var i;
for(i in this){
    if(this[i].toString() == x.toString()){
        this.splice(i,1)
    }
  }
 }


----> Now we can use it like this :

var val=10;
myarray.remove(val);

#1


90  

Please see Defining your own functions in jQuery:

请参阅在jQuery中定义自己的函数:

In this post i want to present how easy define your own functions in jQuery and using them.

在这篇文章中,我想介绍如何在jQuery中轻松定义自己的函数并使用它们。

From the post:

从帖子:

jQuery.fn.yourfunctionname = function() {
    var o = $(this[0]) // It's your element

    return this; // This is needed so others can keep chaining off of this
};

Used:

用过的:

$(element).yourfunctionname()

#2


30  

This is the pattern that I prefer to define my own plugins.

这是我更喜欢定义自己的插件的模式。

(function($) {

    $.fn.extend({
        myfunc: function(options) {
            options = $.extend( {}, $.MyFunc.defaults, options );

            this.each(function() {
                new $.MyFunc(this,options);
            });
            return this;
        }
    });

    // ctl is the element, options is the set of defaults + user options
    $.MyFunc = function( ctl, options ) {
         ...your function.
    };

    // option defaults
    $.MyFunc.defaults = {
        ...hash of default settings...
    };

})(jQuery);

Applied as:

应用为:

$('selector').myfunc( { option: value } );

#3


17  

The jquery documentation has a section on plugin authoring, where I found this example:

jquery文档有一个关于插件创作的部分,我在这里找到了这个例子:

jQuery.fn.debug = function() {
  return this.each(function(){
    alert(this);
  });
};

Then you'd be able to call it this way:

然后你就可以这样称呼它:

$("div p").debug();

#4


11  

jQuery has the extend function to do that

jQuery有扩展功能来做到这一点

jQuery.fn.extend({
  check: function() {
    return this.each(function() { this.checked = true; });
  },
  uncheck: function() {
    return this.each(function() { this.checked = false; });
  }
});

you can see the documentation there

你可以在那里看到文档

#5


3  

Here's a plugin, in it's simplest form...

这是一个插件,最简单的形式......

jQuery.fn.myPlugin = function() {
  // do something here
};

You'll really want to consult the documentation though:

您真的想查阅文档:

http://docs.jquery.com/Plugins/Authoring

http://docs.jquery.com/Plugins/Authoring

#6


0  

/* This prototype example allows you to remove array from array */

Array.prototype.remove = function(x) {
var i;
for(i in this){
    if(this[i].toString() == x.toString()){
        this.splice(i,1)
    }
  }
 }


----> Now we can use it like this :

var val=10;
myarray.remove(val);