jQuery UI API - Button Widget
Category
Usage
**Description:** Themeable buttons and sets of buttons.
**Version added:** 1.8
The Button Widget enhances standard form elements such as button, input, and anchor, theming them with appropriate hover and active styles.
In addition to basic buttons, radio buttons and checkboxes (input types radio and checkbox) can also be converted into buttons. The associated label is styled as a button and updates the underlying input when clicked. To work properly, the input needs to be given an id attribute that points to the label's for attribute. Do not place the input inside the label, otherwise it willcause accessibility problems。
To group radio buttons, Button also provides an additional widget called Buttonset. A Buttonset is used by selecting a container element (containing the radio buttons) and calling .buttonset() on it. Buttonset also provides visual grouping, so consider using it whenever you have a group of buttons. It selects all descendants and applies .button()to them. You can enable and disable a buttonset, which enables and disables all contained buttons. Destroying a buttonset invokes each button's destroy method. For grouped radio and checkbox buttons, it is recommended to use a legend 's fieldset to provide an accessible group label.
When using an input of type button, submit, or reset, only plain-text labels without icons are supported.
Theming
The Button Widget uses the jQuery UI CSS Framework to define its look-and-feel styles. If you need to use button-specific styles, the following CSS class names are available:
ui-button: the DOM element representing the button. Depending on the text and icons option, this element is given one of the following classes:ui-button-text-only、ui-button-icon-only、ui-button-icons-only、ui-button-text-icons。ui-button-icon-primary: the element used to display the button's primary icon. It is present only when the primary icon is provided in the icons option.ui-button-text: the container around the button's text content.ui-button-icon-secondary: the element used to display the button's secondary icon. It is present only when the secondary icon is provided in the icons option.
ui-buttonset: the outer container of the Buttonset.
Depends on
Additional Notes
- This widget requires some functional CSS; otherwise it will not work. If you create a custom theme, use the widget-specific CSS file as a starting point.
Quick navigation
| Option | Method | Events |
|---|---|---|
| Option | Type | Description | Default value |
|---|---|---|---|
| disabled | Boolean | If set to true, the button is disabled. |
代码实例:
初始化带有指定 disabled 选项的 button:
$( ".selector" ).button({ disabled: true });
After initialization, get or set disabled Option:
// getter var disabled = $( ".selector" ).button( "option", "disabled" );| false| onSelect | event | icons | Object | The icons to display, including icons with text and icons without text (see// setter $( ".selector" ).button( "option", "disabled", true );
text options). By default, the primary icon is displayed to the left of the label text and the secondary icon to the right. The display position can be controlled through CSS.
primary and secondary The property value must be icon class name, for example,"ui-icon-gear". If only one icon is used, then icons: { primary: "ui-icon-locked" }. If two icons are used, then icons: { primary: "ui-icon-gear", secondary: "ui-icon-triangle-1-s" }。
Code example:
Initializes with the specified icons The button option:
$( ".selector" ).button({ icons: { primary: "ui-icon-gear", secondary: "ui-icon-triangle-1-s" } });
After initialization, get or set disabled Option:
// getter var icons = $( ".selector" ).button( "option", "icons" );| { primary: null, secondary: null }| onSelect | event | label | String | The text to display in the button. When not specified (// setter $( ".selector" ).button( "option", "icons", { primary: "ui-icon-gear", secondary: "ui-icon-triangle-1-s" } );
null), then use the HTML content of the element, or if the element is an input element of type submit or reset, use its value property, or if the element is an input element of type radio or checkbox, use the HTML content of the associated label element.
Code example:
Initializes with the specified label The button option:
$( ".selector" ).button({ label: "custom label" });
After initialization, get or set label Option:
// getter var label = $( ".selector" ).button( "option", "label" );| null| onSelect | event | text | Boolean | Whether to show the Label. When set to// setter $( ".selector" ).button( "option", "label", "custom label" );
false when, no text is displayed, but it must be enabled at this point icons option, otherwise text The option will be ignored.
Code example:
Initializes with the specified text The button option:
$( ".selector" ).button({ text: false });
After initialization, get or set text Option:
// getter var text = $( ".selector" ).button( "option", "text" );| true |// setter $( ".selector" ).button( "option", "text", false );
| Method | Return | Description |
|---|---|---|
| destroy() | jQuery (plugin only) | Completely removes the button functionality. This returns the element to its pre-initialization state. |
Code example:
Call the destroy method:
$( ".selector" ).button( "destroy" );| onSelect | event | disable() | jQuery (plugin only) | Disable the button.
- This method accepts no arguments.
Code example:
Call the disable method:
$( ".selector" ).button( "disable" );| onSelect | event | enable() | jQuery (plugin only) | Enable the button.
- This method accepts no arguments.
Code example:
Call the enable method:
$( ".selector" ).button( "enable" );| onSelect | event | option( optionName ) | Object | Get the value currently associated with the specified `optionName`.
- optionName
Type: String
Description: the name of the option to get.
Code example:
Call this method:
var isDisabled = $( ".selector" ).button( "option", "disabled" );| onSelect | event | option() | PlainObject | Get an object containing key/value pairs that represent the current button options hash.
- This method accepts no arguments.
Code example:
Call this method:
var options = $( ".selector" ).button( "option" );| onSelect | event | option( optionName, value ) | jQuery (plugin only) | Set the value of the button option associated with the specified `optionName`.
- optionName
Type: String
Description: the name of the option to set. - value
Type: Object
Description: The value to set for the option.
Code example:
Call this method:
$( ".selector" ).button( "option", "disabled", true );| onSelect | event | option( options ) | jQuery (plugin only) | Set one or more options for the button.
- options
Type: Object
Description: The option-value pairs to set.
Code example:
Call this method:
$( ".selector" ).button( "option", { disabled: true } );
| onSelect | event | refresh() | jQuery (plugin only) | Refresh the visual state of the button. Used to update the button state after programmatically changing the checked or disabled state of the native element.
- This method accepts no arguments.
Code example:
Call the refresh method:
$( ".selector" ).button( "refresh" );| onSelect | event | widget() | jQuery | Return a `jQuery` object containing the button.
- This method accepts no arguments.
Code example:
Call the widget method:
var widget = $( ".selector" ).button( "widget" );|
| Events | Type | Description |
|---|---|---|
| create( event, ui ) | buttoncreate | Triggered when the button is created. |
Type: Event
Type: Object
Note:ui The object is empty; it is included here for consistency with other events.
Code example:
Initialize the button with the specified create callback:
$( ".selector" ).button({
create: function( event, ui ) {}
});
Bind an event listener to the buttoncreate event:
$( ".selector" ).on( "buttoncreate", function( event, ui ) {} );
|
Example
Example 1:
A simple jQuery UI Button.
按钮部件(Button Widget)演示
Example 2:
A simple jQuery UI Buttonset.
按钮部件(Button Widget)演示