Menu Widgetversion added: 1.9
Description: Themeable menu with mouse and keyboard interactions for navigation.
A menu can be created from any valid markup as long as the elements have a strict parent/child relationship and each menu item has an anchor. The most commonly used element is the unordered list (<ul>
):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
|
If you use a structure other than <ul>
/<li>
, including using the same element for the menu and the menu items, use the menus
option to specify a way to differentiate the two elements, e.g., menus: "div.menuElement"
.
Any menu item can be disabled by adding the ui-state-disabled
class to that element.
Icons
To add icons to the menu, include them in the markup:
1
2
3
|
|
Menu automatically adds the necessary padding to items without icons.
Dividers
Divider elements can be created by including unlinked menu items that contain only spaces and/or dashes:
1
2
3
4
5
|
|
Keyboard interaction
- ENTER/SPACE: Invoke the focused menu item's action, which may be opening a submenu.
- UP: Move focus to the previous menu item.
- DOWN: Move focus to the next menu item.
- RIGHT: Open the submenu, if available.
- LEFT: Close the current submenu and move focus to the parent menu item. If not in a submenu, do nothing.
- ESCAPE: Close the current submenu and move focus to the parent menu item. If not in a submenu, do nothing.
Typing a letter moves focus to the first item whose title starts with that character. Repeating the same character cycles through matching items. Typing more characters within the one second timer matches those characters.
Disabled items can receive keyboard focus, but do not allow any other interaction.
Theming
The menu widget uses the jQuery UI CSS framework to style its look and feel. If menu specific styling is needed, the following CSS class names can be used:
-
ui-menu
: The outer container of the menu. This element will additionally have aui-menu-icons
class if the menu contains icons.-
ui-menu-item
: The container for individual menu items.-
ui-menu-icon
: The submenu icons set via theicons
option.
-
-
ui-menu-divider
: Divider elements between menu items.
-
Dependencies
Additional Notes:
- This widget requires some functional CSS, otherwise it won't work. If you build a custom theme, use the widget's specific CSS file as a starting point.
Options
disabled
false
true
.Initialize the menu with the disabled
option specified:
1
2
3
|
|
Get or set the disabled
option, after initialization:
1
2
3
4
5
|
|
icons
{ submenu: "ui-icon-carat-1-e" }
- submenu (string, default: "ui-icon-carat-1-e")
Initialize the menu with the icons
option specified:
1
2
3
|
|
Get or set the icons
option, after initialization:
1
2
3
4
5
|
|
position
{ my: "left top", at: "right top" }
of
option defaults to the parent menu item, but you can specify another element to position against. You can refer to the jQuery UI Position utility for more details about the various options.Initialize the menu with the position
option specified:
1
2
3
|
|
Get or set the position
option, after initialization:
1
2
3
4
5
|
|
role
"menu"
Customize the ARIA roles used for the menu and menu items. The default uses "menuitem"
for items. Setting the role
option to "listbox"
will use "option"
for items. If set to null
, no roles will be set, which is useful if the menu is being controlled by another element that is maintaining focus.
role
option should not be changed after initialization. Existing (sub)menus and menu items will not be updated.Initialize the menu with the role
option specified:
1
2
3
|
|
Get the role
option, after initialization:
1
2
|
|
Methods
blur( [event ] )Returns: jQuery (plugin only)
blur
event.
-
eventType: EventWhat triggered the menu to blur.
Invoke the blur method:
1
|
|
collapse( [event ] )Returns: jQuery (plugin only)
-
eventType: EventWhat triggered the menu to collapse.
Invoke the collapse method:
1
|
|
collapseAll( [event ] [, all ] )Returns: jQuery (plugin only)
-
eventType: EventWhat triggered the menu to collapse.
-
allType: BooleanIndicates whether all sub-menus should be closed or only sub-menus below and including the menu that is or contains the target of the triggering event.
Invoke the collapseAll method:
1
|
|
destroy()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the destroy method:
1
|
|
disable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the disable method:
1
|
|
enable()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the enable method:
1
|
|
expand( [event ] )Returns: jQuery (plugin only)
-
eventType: EventWhat triggered the menu to expand.
Invoke the expand method:
1
|
|
focus( [event ], item )Returns: jQuery (plugin only)
focus
event.
-
eventType: EventWhat triggered the menu item to gain focus.
-
itemType: jQueryThe menu item to focus/activate.
Invoke the focus method:
1
|
|
isFirstItem()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the isFirstItem method:
1
|
|
isLastItem()Returns: jQuery (plugin only)
- This method does not accept any arguments.
Invoke the isLastItem method:
1
|
|
next( [event ] )Returns: jQuery (plugin only)
-
eventType: EventWhat triggered the focus to move.
Invoke the next method:
1
|
|
nextPage( [event ] )Returns: jQuery (plugin only)
-
eventType: EventWhat triggered the focus to move.
Invoke the nextPage method:
1
|
|
option( optionName )Returns: Object
optionName
.-
optionNameType: StringThe name of the option to get.
Invoke the method:
1
|
|
option()Returns: PlainObject
- This signature does not accept any arguments.
Invoke the method:
1
|
|
option( optionName, value )Returns: jQuery (plugin only)
optionName
.-
optionNameType: StringThe name of the option to set.
-
valueType: ObjectA value to set for the option.
Invoke the method:
1
|
|
option( options )Returns: jQuery (plugin only)
-
optionsType: ObjectA map of option-value pairs to set.
Invoke the method:
1
|
|
previous( [event ] )Returns: jQuery (plugin only)
-
eventType: EventWhat triggered the focus to move.
Invoke the previous method:
1
|
|
previousPage( [event ] )Returns: jQuery (plugin only)
-
eventType: EventWhat triggered the focus to move.
Invoke the previousPage method:
1
|
|
refresh()Returns: jQuery (plugin only)
refresh()
method.
- This method does not accept any arguments.
Invoke the refresh method:
1
|
|
select( [event ] )Returns: jQuery (plugin only)
select
event.
-
eventType: EventWhat triggered the selection.
Invoke the select method:
1
|
|
widget()Returns: jQuery
jQuery
object containing the menu.
- This method does not accept any arguments.
Invoke the widget method:
1
|
|
Events
blur( event, ui )Type: menublur
Initialize the menu with the blur callback specified:
1
2
3
|
|
Bind an event listener to the menublur event:
1
|
|
create( event, ui )Type: menucreate
Note: The ui
object is empty but included for consistency with other events.
Initialize the menu with the create callback specified:
1
2
3
|
|
Bind an event listener to the menucreate event:
1
|
|
focus( event, ui )Type: menufocus
Initialize the menu with the focus callback specified:
1
2
3
|
|
Bind an event listener to the menufocus event:
1
|
|
select( event, ui )Type: menuselect
Initialize the menu with the select callback specified:
1
2
3
|
|
Bind an event listener to the menuselect event:
1
|
|
Example:
A simple jQuery UI Menu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
|