A simple example of the extended interface of jquery

Source: Internet
Author: User

As a lightweight JS framework, jquery has its unique advantages. A lot of web programmers are bored with it. In this framework, in addition to the rich client processing capabilities, animation features. It also provides a very customizable extension interface to facilitate more people to develop and expand jquery. Show this interface in a simple example. I think this is enough to make a lot of people understand the way. The following example refers to the API from jquery.

1 $.extend({
2     max: function(a, b) {
3         return a > b ? a : b;
4     },
5     min: function(a, b) {
6         return a > b ? b : a;
7     },
8     avg: function(a, b) {
9         return a / b;
10     }
11 });

The example is used to add a new function in jquery, which is a static function.

The call is as follows:

jQuery.min(2,3); // => 2
jQuery.max(4,5); // => 5

The same is true if the function extension functions for a component are also simple. For example, you want to extend the function of the textbox so that it is highlighted when the focus is obtained, and the highlight is canceled when the focus is lost. Of course, the effect of highlighting can be implemented using CSS, so you can invoke the name of a color as a parameter. The code is as follows:

$.fn.hightlight = function(colorName) {
this.mouseover(function() {
$(this).css('background-color', colorName); //this对是对组件自 身的引用
});
this.mouseout(function() {
$(this).css('background-color', '');
});
}

The call is as follows:

$(function() {
$('#test').hightlight('red');
});

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.