Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
78 views
in Technique[技术] by (71.8m points)

How to add a function to jQuery?

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

So that I can call something like:

$('#id').applyMyOwnFunc()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Please see "Defining your own functions in jQuery" by Basil Goldman:

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

Modified, based on the code in the blog post linked above:

jQuery.fn.yourFunctionName = function() {
    // `this` is the jQuery Object on which the yourFunctionName method is called.
    // `arguments` will contain any arguments passed to the yourFunctionName method.
    var firstElement = this[0];

    return this; // Needed for other methods to be able to chain off of yourFunctionName.
};

Just use:

$(element).yourFunctionName();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...