jquery Plugin Authoring

Source: Internet
Author: User
Tags extend

/*
Hello Everybody, let's write a jquery plugin, do not always use jquery, but even plug-ins can not write.
Ha ha, joking.
When you see this log, you can be sure that you are a fan of jquery.
Say JS is very casual language, very casually, want to write how to write.
And look at this code.
*/
var jquery = new Object ();//or var jquery = {};
Jquery.method = function (args) {
Alert ("Hello," + args);
}
Jquery.method ("Henry");
/*
Hey, too obvious, you'll see "Hello,henry" pop-up messages. Is this code huge and simple?
Of course, you will naturally think of this method. As follows:
*/
<script language= "javascript" src= "Jquery.js" ></script>
<script language= "JavaScript" >
Jquery.method = function () {
Alert ("Hello,this is Me defined method");
}
Jquery.method ();
</script>
/*
The effect is as you wish. But not the best. Because, a good frame must be left to the Hello interface.
It is best to use it. Should be as follows:
*/
Jquery.extend ({
Method:function () {
Alert ("Hello,this is Me defined method");
}
});
Jquery.method ();
/*
Good, the effect is still the same. But this is the best, why? Because, when your function needs
When adding a parameter, you need to use the default parameter if the other person does not add the parameter. At this time, your design
The benefit is coming, the IT ^.^:
*/

Jquery.extend ({
Method:function (options) {
var defaults = {
Name: ' Henry ',
Sex: ' Male '
};
Jqu Ery.extend (defaults,options); So, all your parameters are coming in.
Alert ("Hello," + Defaults.name + ", you are" + defaults.sex);
}
});
Jquery.method ();
Jquery.method ({
Name: "Yourname",
Sex: "Sex"
});
//Anyway, you can invoke the plugin I wrote. Oh, oh, yes.
/*
There is also the HA, need to remind you, if you read my written analysis of jquery source code or the analysis of their own jquery source code
You will have different feelings. The above is an extension of the static method, which means you don't have to select any element to use the method directly through the
jquery class. The following describes how an element is manipulated. You simply change the Jquery.extend () to the
JQuery.fn.extend ();
*/
JQuery.fn.extend ({
Method:function (options) {
var defaults = {
Name: "Henry",
Sex: "Male"
};
Jquery.extend (defaults,options); So, all your parameters are coming in.
Alert ("Hello," + Defaults.name + ", you are" + defaults.sex);
Return $ (this);/Don't forget to go back and implement the link call.
}
});

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.