jquery Plugin Development Matters

Source: Internet
Author: User

    • Http://www.cnblogs.com/Wayou/p/jquery_plugin_tutorial.html
    • http://www.w2bc.com/Article/13951
    • Http://www.w3cschool.cc/w3cnote/jquery-10-skill.html
This

Inside the function defined by the plug-in name, this refers to the element that we select with the jquery selector when invoking the plugin, which is generally a collection of jquery types. For example, $ (' a ') returns a collection of all the A tags on the page, and the collection is already a jquery wrapper type, which means that you can invoke other methods of jquery directly when you manipulate it without having to wrap it with a dollar sign.

We already know that this refers to the collection returned by the jquery selector, so each element of the collection can be processed by invoking the. each () method of jquery, but it is important to note that within each method, this refers to the normal DOM element. If you need to call jquery's method then you need to re-wrap it with $.

$.fn.button = function (option)    {//this refers to the object returned by jquery. Return This.each (function ()        {            var $this = $ (this)//here only the DOM element is obtained, so you need to encapsulate            var data = $this. Data (' Zui.button ')            var options = typeof option = ' Object ' && option            if (!data) $this. Data (' Zui.button ', (data = New button (this, options)))            if (option = = ' t Oggle ') Data.toggle ()            else if (option) data.setstate (option)        })    }



Object-oriented plug-in case

;(function ($, window, document,undefined) {    //define Beautifier's constructor    var beautifier = function (Ele, opt) {this        . $element = Ele,        this.defaults = {            ' color ': ' Red ',            ' fontSize ': ' 12px ',            ' textdecoration ': ' None '        } ,        this.options = $.extend ({}, this.defaults, opt)    }    //method for defining Beautifier    Beautifier.prototype = {        beautify:function () {            return this. $element. css ({                ' color ': This.options.color,                ' fontSize ': This.options.fontSize,                ' textdecoration ': This.options.textDecoration            });        }    }    Using the Beautifier object in the plug-in    $.fn.myplugin = function (options) {        //create Beautifier entity        var beautifier = new Beautifier (this, options);        Call its method        return beautifier.beautify ();    }}) (JQuery, window, document);



JQuery plug-in development matters

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.