This example demonstrates how to listen for DOM-related events. Interaction with the Menu will result in event information being output to the console. Please note: Disabled MenuItem instances do not fire DOM events. This is demonstrated with the MenuItem named "MenuItem 2."
Note: By default clicking outside of a Menu instance will hide it. Additionally, MenuItem instances without a submenu or a URL to navigate to will hide their parent Menu instance when clicked. Click the "Show Menu" button below to make the Menu instance visible if it is hidden.
All of the events for Menu and MenuItem (including DOM-based events such as
"mouseover" or "click") are implemented as instances of
Custom Event
(YAHOO.util.CustomEvent
). Please note: To
listen for DOM-based events always use the provided Custom Event-based interface
rather than attaching handlers directly to a Menu's DOM elements.
Add event listeners through the subscribe
method. The following
example demonstrates how to subscribe to the "show" and "hide" events:
1 | /* |
2 | Generic event handler used to log info about the DOM events for |
3 | the Menu instance. |
4 | */ |
5 | |
6 | function onMenuEvent(p_sType, p_aArgs) { |
7 | |
8 | var oDOMEvent = p_aArgs[0]; |
9 | |
10 | YAHOO.log(("Id: " + this.id + ", " + |
11 | "Custom Event Type: " + p_sType + ", " + |
12 | "DOM Event Type: " + oDOMEvent.type), |
13 | "info", "example10"); |
14 | } |
15 | |
16 | |
17 | /* |
18 | Generic event handler used to log info about the DOM events for |
19 | each MenuItem instance. |
20 | */ |
21 | |
22 | function onMenuItemEvent(p_sType, p_aArgs) { |
23 | |
24 | var oDOMEvent = p_aArgs[0]; |
25 | |
26 | YAHOO.log(("Index: " + this.index + ", " + |
27 | "Group Index: " + this.groupIndex + ", " + |
28 | "Custom Event Type: " + p_sType + ", " + |
29 | "DOM Event Type: " + oDOMEvent.type |
30 | ), "info", "example10"); |
31 | |
32 | } |
33 | |
34 | |
35 | /* |
36 | Instantiate a Menu: The first argument passed to the constructor |
37 | is the id for the Menu element to be created, the second is an |
38 | object literal of configuration properties. |
39 | */ |
40 | |
41 | var oMenu = new YAHOO.widget.Menu("basicmenu", { fixedcenter: true }); |
42 | |
43 | |
44 | // Subscribe to the Menu instance's "itemAdded" event |
45 | |
46 | oMenu.subscribe("itemAdded", function (p_sType, p_aArgs) { |
47 | |
48 | var oMenuItem = p_aArgs[0]; |
49 | |
50 | /* |
51 | Subscribe to each MenuItem instance's DOM events as they |
52 | are added to the Menu instance. |
53 | */ |
54 | |
55 | oMenuItem.subscribe("mouseover", onMenuItemEvent); |
56 | oMenuItem.subscribe("mouseout", onMenuItemEvent); |
57 | oMenuItem.subscribe("mousedown", onMenuItemEvent); |
58 | oMenuItem.subscribe("mouseup", onMenuItemEvent); |
59 | oMenuItem.subscribe("click", onMenuItemEvent); |
60 | oMenuItem.subscribe("keydown", onMenuItemEvent); |
61 | oMenuItem.subscribe("keyup", onMenuItemEvent); |
62 | oMenuItem.subscribe("keypress", onMenuItemEvent); |
63 | |
64 | }); |
65 | |
66 | |
67 | // Subscribe to every DOM event for the Menu instance. |
68 | |
69 | oMenu.subscribe("mouseover", onMenuEvent); |
70 | oMenu.subscribe("mouseout", onMenuEvent); |
71 | oMenu.subscribe("mousedown", onMenuEvent); |
72 | oMenu.subscribe("mouseup", onMenuEvent); |
73 | oMenu.subscribe("click", onMenuEvent); |
74 | oMenu.subscribe("keydown", onMenuEvent); |
75 | oMenu.subscribe("keyup", onMenuEvent); |
76 | oMenu.subscribe("keypress", onMenuEvent); |
77 | |
78 | |
79 | /* |
80 | Add items to the Menu instance by passing an array of object literals |
81 | (each of which represents a set of YAHOO.widget.MenuItem |
82 | configuration properties) to the "addItems" method. |
83 | */ |
84 | |
85 | oMenu.addItems([ |
86 | |
87 | "MenuItem 0", |
88 | |
89 | "MenuItem 1", |
90 | |
91 | /* |
92 | Add a disabled menu item to demonstrate that disabled |
93 | items do not respond to DOM events. |
94 | */ |
95 | { text: "MenuItem 2", disabled: true }, |
96 | |
97 | "MenuItem 3", |
98 | |
99 | "MenuItem 4" |
100 | |
101 | ]); |
102 | |
103 | |
104 | /* |
105 | Since this Menu instance is built completely from script, call the |
106 | "render" method passing in the DOM element that it should be |
107 | appended to. |
108 | */ |
109 | |
110 | oMenu.render("rendertarget"); |
view plain | print | ? |
You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.
INFO 762ms (+0) 12:37:34 AM:
MenuManager
MenuItem yui-gen4 successfully registered.
INFO 762ms (+1) 12:37:34 AM:
Menu basicmenu
Item added. Text: MenuItem 4, Index: 4, Group Index: 0
INFO 761ms (+0) 12:37:34 AM:
MenuManager
MenuItem yui-gen3 successfully registered.
INFO 761ms (+0) 12:37:34 AM:
Menu basicmenu
Item added. Text: MenuItem 3, Index: 3, Group Index: 0
INFO 761ms (+0) 12:37:34 AM:
MenuManager
MenuItem yui-gen2 successfully registered.
INFO 761ms (+0) 12:37:34 AM:
Menu basicmenu
Item added. Text: MenuItem 2, Index: 2, Group Index: 0
INFO 761ms (+0) 12:37:34 AM:
MenuManager
MenuItem yui-gen1 successfully registered.
INFO 761ms (+0) 12:37:34 AM:
Menu basicmenu
Item added. Text: MenuItem 1, Index: 1, Group Index: 0
INFO 761ms (+1) 12:37:34 AM:
MenuManager
MenuItem yui-gen0 successfully registered.
INFO 760ms (+1) 12:37:34 AM:
Menu basicmenu
Item added. Text: MenuItem 0, Index: 0, Group Index: 0
INFO 759ms (+0) 12:37:34 AM:
MenuManager
Menu basicmenu successfully registered.
INFO 759ms (+0) 12:37:34 AM:
MenuManager
DOM event handlers initialized.
INFO 759ms (+3) 12:37:34 AM:
Menu basicmenu
No source element found. Created element with id: basicmenu
INFO 756ms (+756) 12:37:34 AM:
LogReader instance0
LogReader initialized
INFO 0ms (+0) 12:37:33 AM:
global
Logger initialized
Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings