Jquery UI is an open-source library officially maintained by jquery.
Jquery UI is inherited from the jquery. UI. widget. js file. This file provides a factory method to create a widget object. The method is $. widget (string name, options prototype). The following describes the methods and Attributes provided by this class. These will be overwritten when creating widgets.
The _ create () method is used to create Widgets. When a widget is called on a page, this method is executed to build widgets. Most of the behaviors and structures of widgets are created here. This method is created only once.
_ Init () is not overwritten most of the time. This method is executed after _ create when building widgets. If you have written this method, this method will be executed every time you run it.
Destroy (): removes a widget instance from a DOM object. This method is generally required when developing widgets. Removes the styles, actions, and Dom structures you have added on the DOM element.
Options: the configuration information of the widget is saved here. You need to set some configuration parameters when creating the widget.
Element: the DOM object used by the widget.
Enable () and disable () are used to disable and enable widgets. It is actually modifying options. Disabled.
The following uses creating a simple object as an example:
(Function ($, window, undefined) {$. widget ("UI. test ", {options: {width: 400}, _ create: function () {alert (" first run create object ")}, _ init: function () {alert ("instantiate object each time")}, getwidth: function () {return this. options. width;}, _ getwidth: function () {return this. options. width; // Private method}, destroy: function () {alert ("before the object is deleted from the DOM") }});}) (jquery, window, undefined)
In UI. Test, the UI is the namespace, and test is the object name. If the page has an element <Div id = "test"/>
Run $ ("# test"). Test ().
Alert ("first run create object ")
Alert ("instantiate objects each time ")
You can run the method $ ("# test") once the instance is instantiated "). test ("getwidth") can be used when the method has parameters $ ("# test "). test ("getwidth", parameter 1, parameter 2)
The options value is not set to the default value. In this example, 400 can be set at the first run, for example, $ ("# test "). test ({width: 500}). You can use
$ ("# Test"). Test ("option", "width", 500) change its parameter value.
A private method starts with "_" and cannot be called externally. If $ ("# test"). Test ("_ getwidth") is run in this example, an error will occur.
For example, running $ ("# test"). Remove () will execute alert ("before the object is deleted from the DOM ")