Writing jquery Plugins-3

Source: Internet
Author: User

We know the same thing, and then we'll write our first JQ plugin.

Plug-in for encapsulating object methods

We write a plugin that sets and gets the color, we need to implement two functions

1. Set the color of the matching element

2. Gets the color of the matching element (the first of the element collection)

We use JQuery.fn.extend (), the code is as follows:

;(function ($) {    /**/}) (JQuery)

This is the outermost architecture, next;

   ;(Function ($) {        $.fn.extend ({            color:function (val) {                ///  code             }        });    }) (jQuery)

I'll add the corresponding code in there.

;(function ($) {        $.fn.extend ({            color:function (val) {                console.log (val)}                ; if (val = = undefined)                    {returnthis. css ('color') ;                }                     Else {                    returnthis. css ('color', Val);                }            }        });    }) (jQuery)

We use the JQ css () method and get the color value, the plug-in's this point is the JQ object, if the plug-in does not return a specific value or string, it should be made to be connected, to return this object directly, because the CSS () method defines the return object, directly return can;

Encapsulate our jq.color.js.

The page calls the JS

Console.log ($ ("div"). Color ());
Console.log ($ ("div"). Color ("red"));

Writing jquery Plugins-3

Related Article

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.