Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery UI API - Accordion Widget

jQuery UI API - Accordion Widget

Category

Widgets

Usage

**Description:** Turns a pair of header and content panels into an accordion.

**Version added:** 1.0

The markup of the accordion container requires a pair of header and content panels.



First header

First content panel

Second header

Second content panel

The accordion supports arbitrary markup, but each content panel must be the next sibling of its associated header panel. See header the options for how to use a custom markup structure.

Panels can be activated programmatically by setting the active option.

Keyboard interaction

When focus is on a header, the following keyboard commands are available:

When focus is on a panel, the following keyboard commands are available:

Theming

The Accordion widget uses the jQuery UI CSS Framework to define its look-and-feel styles. If you need to use accordion-specific styles, the following CSS class names are available:

- `ui-accordion-header`: the header of the accordion. If the header contains [`icons`](#option-icons), the header will additionally have a `ui-accordion-icons` class. - `ui-accordion-content`: the content panel of the accordion.

Depends on

Additional Notes

Quick navigation

Option Method Events

active

animate
collapsible
disabled
event
header
heightStyle
icons
|
destroy
disable
enable
option
refresh
widget
|
activate
beforeActivate
create
|
Option Type Description Default value
active Boolean or Integer Which panel is currently open.
 

支持多个类型:

  • Boolean: set the active is false Collapses all panels. This requires the collapsible option to be true。
  • Integer: the index of the panel to activate, zero-based. Negative values select a panel counting back from the last panel.
  • Code example:

    Initialize the accordion with the specified active option:

    $( ".selector" ).accordion({ active: 2 });
    
        

    After initialization, get or set the active Option:

    // getter
    
    var active = $( ".selector" ).accordion( "option", "active" );
    
    

    // setter $( ".selector" ).accordion( "option", "active", 2 );     

    | 0| onSelect | event | animate | Boolean or Number or String or Object | Whether to use animation to change panels, and how to animate the change.

    Multiple types are supported:

    Code example:

    Initialize the accordion with the specified animate option:

    $( ".selector" ).accordion({ animate: "bounceslide" });
    
        

    After initialization, get or set the animate Option:

    // getter
    
    var animate = $( ".selector" ).accordion( "option", "animate" );
    
    

    // setter $( ".selector" ).accordion( "option", "animate", "bounceslide" );     

    | {}| onSelect | event | collapsible | Boolean | Whether all sections can be closed immediately. Allows the active section to be collapsed.

    Code example:

    Initialize the accordion with the specified collapsible option:

    $( ".selector" ).accordion({ collapsible: true });
    
        

    After initialization, get or set the collapsible Option:

    // getter
    
    var collapsible = $( ".selector" ).accordion( "option", "collapsible" );

    // setter $( ".selector" ).accordion( "option", "collapsible", true );     

    | false| onSelect | event | disabled | Boolean | If set to true, the accordion is disabled.

    Code example:

    Initialize the accordion with the specified disabled option:

    $( ".selector" ).accordion({ disabled: true });
    
        

    After initialization, get or set the disabled Option:

    // getter
    
    var disabled = $( ".selector" ).accordion( "option", "disabled" );

    // setter $( ".selector" ).accordion( "option", "disabled", true );     

    | false| onSelect | event | event | String | The event the accordion header responds to in order to activate the associated panel. Multiple events can be specified, separated by spaces.

    Code example:

    Initialize the accordion with the specified event option:

    $( ".selector" ).accordion({ event: "mouseover" });
    
        

    After initialization, get or set the event Option:

    // getter
    
    var event = $( ".selector" ).accordion( "option", "event" );

    // setter $( ".selector" ).accordion( "option", "event", "mouseover" );     

    | "click"| onSelect | event | header | Selector | Selector for the header elements, applied via .find() on the main accordion element. A content panel must be a sibling immediately following its associated header.

    Code example:

    Initialize the accordion with the specified header option:

    $( ".selector" ).accordion({ header: "h3" });
    
        

    After initialization, get or set the header Option:

    // getter
    
    var header = $( ".selector" ).accordion( "option", "header" );

    // setter $( ".selector" ).accordion( "option", "header", "h3" );     

    | "> li > :first-child,> :not(li):even"| onSelect | event | heightStyle | String | Controls the height of the accordion and each panel. Possible values:

    Code example:

    Initialize the accordion with the specified heightStyle option:

    $( ".selector" ).accordion({ heightStyle: "fill" });
    
        

    After initialization, get or set the heightStyle Option:

    // getter
    
    var heightStyle = $( ".selector" ).accordion( "option", "heightStyle" );
    
    

    // setter $( ".selector" ).accordion( "option", "heightStyle", "fill" );     

    | "auto"| onSelect | event | icons | Object | Icons to use for the headers, matching the Icons provided by the jQuery UI CSS Framework matching. Set to false to display no icons.

    Code example:

    Initialize the accordion with the specified icons option:

    $( ".selector" ).accordion({ icons: { "header": "ui-icon-plus", "activeHeader": "ui-icon-minus" } });
    
        

    After initialization, get or set the icons Option:

    // getter
    
    var icons = $( ".selector" ).accordion( "option", "icons" );
    
    

    // setter $( ".selector" ).accordion( "option", "icons", { "header": "ui-icon-plus", "activeHeader": "ui-icon-minus" } );     

    | { "header": "ui-icon-triangle-1-e", "activeHeader": "ui-icon-triangle-1-s" } |

    Method Return Description
    destroy() jQuery (plugin only) Completely removes the accordion functionality. This returns the element to its pre-initialization state.
     
  • This method accepts no arguments.
  • Code example:

    Invoke the destroy method:

    $( ".selector" ).accordion( "destroy" );
    
        
    | onSelect | event | disable() | jQuery (plugin only) | Disables the accordion.

    Code example:

    Invoke the disable method:

    $( ".selector" ).accordion( "disable" );
    
        
    | onSelect | event | enable() | jQuery (plugin only) | Enables the accordion.

    Code example:

    Invoke the enable method:

    $( ".selector" ).accordion( "enable" );
    
        
    | onSelect | event | option( optionName ) | Object | Gets the value currently associated with the specified `optionName`.

    Code example:

    Invoke the method:

    var isDisabled = $( ".selector" ).accordion( "option", "disabled" );
    
        
    | onSelect | event | option() | PlainObject | Gets an object containing key/value pairs representing the current accordion options hash.

    Code example:

    Invoke the method:

    var options = $( ".selector" ).accordion( "option" );
    
        
    | onSelect | event | option( optionName, value ) | jQuery (plugin only) | Sets the value of the accordion option associated with the specified `optionName`.

    Code example:

    Invoke the method:

    $( ".selector" ).accordion( "option", "disabled", true );
    
        
    | onSelect | event | option( options ) | jQuery (plugin only) | Sets one or more options for the accordion.

    Code example:

    Invoke the method:

    $( ".selector" ).accordion( "option", { disabled: true } );
    
        
    | onSelect | event | refresh() | jQuery (plugin only) | Processes any headers and panels added or removed directly in the DOM, and recalculates the height of the accordion. The result depends on the content and the `heightStyle` option.

    Code example:

    Invoke the refresh method:

    $( ".selector" ).accordion( "refresh" );
    
        
    | onSelect | event | widget() | jQuery | Returns a `jQuery` object containing the accordion.

    Code example:

    Invoke the widget method:

    var widget = $( ".selector" ).accordion( "widget" );
    
        
    |
    Events Type Description
    activate( event, ui ) accordionactivate Triggered after a panel is activated (after the animation completes). If the accordion was collapsed, then ui.oldHeader and ui.oldPanel will be an empty jQuery object. If the accordion is collapsing, then ui.newHeader and ui.newPanel will be an empty jQuery object.
     

    注意:由于 activate 事件只有在面板激活时才能触发,当创建 accordion 部件时,最初的面板不会触发该事件。如果您需要一个用于部件创建的钩,请使用 create 事件。

  • event
    Type: Event
  • ui
    Type: Object
    • newHeader
      Type: jQuery
      Description: the header that was just activated.
    • oldHeader
      Type: jQuery
      Description: the header that was just deactivated.
    • newPanel
      Type: jQuery
      Description: the panel that was just activated.
    • oldPanel
      Type: jQuery
      Description: the panel that was just deactivated.
  • Code example:

    Initialize the accordion with the specified activate callback:

    $( ".selector" ).accordion({
    
      activate: function( event, ui ) {}
    
    });
    
        

    Bind an event listener to the accordionactivate event:

    $( ".selector" ).on( "accordionactivate", function( event, ui ) {} );
    
        
    | onSelect | event | beforeActivate( event, ui ) | accordionbeforeactivate | Triggered directly before a panel is activated. Can be canceled to prevent the panel from being activated. If the accordion is currently collapsed, `ui.oldHeader` and `ui.oldPanel` will be empty jQuery objects. If the accordion is collapsing, `ui.newHeader` and `ui.newPanel` will be empty jQuery objects.

    Code example:

    Initialize the accordion with the specified beforeActivate callback:

    $( ".selector" ).accordion({
    
      beforeActivate: function( event, ui ) {}
    
    });
    
        

    Bind an event listener to the accordionbeforeactivate event:

    $( ".selector" ).on( "accordionbeforeactivate", function( event, ui ) {} );
    
        
    | onSelect | event | create( event, ui ) | accordioncreate | Triggered when the accordion is created. If the accordion is collapsed, `ui.header` and `ui.panel` will be empty jQuery objects.

    Code example:

    Initialize the accordion with the specified create callback:

    $( ".selector" ).accordion({
    
      create: function( event, ui ) {}
    
    });
    
        

    Bind an event listener to the accordioncreate event:

    $( ".selector" ).on( "accordioncreate", function( event, ui ) {} );
    
        
    |

    Example

    A simple jQuery UI accordion.

    
    
    
    
    
    
    
    
      
    
      折叠面板部件(Accordion Widget)演示
    
      
    
      
    
      
    
    
    
    
    
     
    
    

    部分 1

    Mauris mauris ante, blandit et, ultrices a, suscipit eget. Integer ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut odio.

    部分 2

    Sed non urna. Phasellus eu ligula. Vestibulum sit amet purus. Vivamus hendrerit, dolor aliquet laoreet, mauris turpis velit, faucibus interdum tellus libero ac justo.

    部分 3

    Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.Phasellus pellentesque purus in massa.

    • List item one
    • List item two
    • List item three