以下是从官网copy过来。 基本上jquery插件都是这个样子。记录下来参考。
<html lang="en">
<title>jQuery UI Widget - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
.custom-colorize-changer {
// the widget definition, where "custom" is the namespace,
// "colorize" the widget name
$.widget( "custom.colorize", {
// add a class for theming
.addClass( "custom-colorize" )
// prevent double click to select text
this.changer = $( "<button>", {
"class": "custom-colorize-changer"
.appendTo( this.element )
// bind click events on the changer button to the random method
this._on( this.changer, {
// _on won't call random when widget is disabled
// called when created, and later when changing options
this.element.css( "background-color", "rgb(" +
this.options.green + "," +
// trigger a callback/event
this._trigger( "change" );
// a public method to change the color to a random value
// can be called directly via .colorize( "random" )
random: function( event ) {
red: Math.floor( Math.random() * 256 ),
green: Math.floor( Math.random() * 256 ),
blue: Math.floor( Math.random() * 256 )
// trigger an event, check if it's canceled
if ( this._trigger( "random", event, colors ) !== false ) {
// events bound via _on are removed automatically
// revert other modifications here
// remove generated elements
.removeClass( "custom-colorize" )
.css( "background-color", "transparent" );
// _setOptions is called with a hash of all options that are changing
// always refresh when changing options
_setOptions: function() {
// _super and _superApply handle keeping the right this-context
this._superApply( arguments );
// _setOption is called for each individual option that is changing
_setOption: function( key, value ) {
// prevent invalid color values
if ( /red|green|blue/.test(key) && (value < 0 || value > 255) ) {
this._super( key, value );
// initialize with default options
$( "#my-widget1" ).colorize();
// initialize with two customized options
$( "#my-widget2" ).colorize({
// initialize with custom green value
// and a random callback to allow only colors with enough green
$( "#my-widget3" ).colorize( {
random: function( event, ui ) {
// click to toggle enabled/disabled
$( "#disable" ).click(function() {
// use the custom selector created for each widget to find all instances
// all instances are toggled together, so we can check the state from the first
if ( $( ":custom-colorize" ).colorize( "option", "disabled" ) ) {
$( ":custom-colorize" ).colorize( "enable" );
$( ":custom-colorize" ).colorize( "disable" );
// click to set options after initalization
$( "#black" ).click( function() {
$( ":custom-colorize" ).colorize( "option", {
<div id="my-widget1">color me</div>
<div id="my-widget2">color me</div>
<div id="my-widget3">color me</div>
<button id="disable">Toggle disabled option</button>
<button id="black">Go black</button>