1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" |
---|
2 | "http://www.w3.org/TR/html4/strict.dtd"> |
---|
3 | <html id="html"> |
---|
4 | <head> |
---|
5 | <title>NodeList.delegate() test</title> |
---|
6 | <style type="text/css"> |
---|
7 | @import "../../../dojo/resources/dojo.css"; |
---|
8 | </style> |
---|
9 | <script type="text/javascript" |
---|
10 | src="../../../dojo/dojo.js" data-dojo-config="async: true, isDebug: true, popup: true"></script> |
---|
11 | <script type="text/javascript"> |
---|
12 | require( |
---|
13 | ["dojo/query", "dojo/dom-class", "dojo/dom-construct", "dojo/dom-style", "dojox/NodeList/delegate", "dojo/domReady!"], |
---|
14 | |
---|
15 | function (query, domClass, domConstruct, domStyle) { |
---|
16 | // Monitor onclick events on div.blue nodes, or that bubble up to div.blue nodes |
---|
17 | query("div.delegator").delegate("div.blue", "onclick", function(evt){ |
---|
18 | // "this" points to the div.blue node |
---|
19 | domStyle.set(this, "backgroundColor", "#aaf"); |
---|
20 | }); |
---|
21 | |
---|
22 | query("div.delegator").delegate("input", "onfocus", function(evt){ |
---|
23 | // "this" points to the input node |
---|
24 | console.log("bubbled input event"); |
---|
25 | domStyle.set(this, "backgroundColor", "black"); |
---|
26 | }); |
---|
27 | |
---|
28 | // Generate div.blue nodes inside each wrapper div. |
---|
29 | // Runs after the delegate() call to demonstrate that delegate() catches events on "future nodes" |
---|
30 | query("div.wrapper").forEach(function(div){ |
---|
31 | for(var i=0; i<4; i++){ |
---|
32 | domConstruct.place( |
---|
33 | "<div class=" + (i%2?"blue":"red") + ">" + |
---|
34 | (i%2 && domClass.contains(div, "delegator") ? "click me to turn me blue" : "click will have no effect" )+ |
---|
35 | "<span>" + (i%2 && domClass.contains(div, "delegator") ? "or click me to turn parent blue" : "nor will a click here") + "</span>"+ |
---|
36 | "focus input to turn it black (not working yet): <input />" + |
---|
37 | "</div>", |
---|
38 | div |
---|
39 | ); |
---|
40 | } |
---|
41 | }); |
---|
42 | }); |
---|
43 | </script> |
---|
44 | <style> |
---|
45 | div, a { padding: 5px; margin: 5px; display: block; } |
---|
46 | div.blue { border: blue solid 2px; } |
---|
47 | div.red { border: red solid 2px; } |
---|
48 | div.wrapper { border: solid gray 4px; background: #eee; } |
---|
49 | div.delegator { background: #ccc; } |
---|
50 | span { display: block; border: yellow solid 2px; } |
---|
51 | </style> |
---|
52 | </head> |
---|
53 | <body id="body" class="classy"> |
---|
54 | <h1>Test of NodeList.delegate() method</h1> |
---|
55 | <div class=blue style="border: 2px dotted black;"> |
---|
56 | <h3> |
---|
57 | This DIV has class=blue but it shouldn't matter because the delegate() connections are on |
---|
58 | sub node inside of me. |
---|
59 | </h3> |
---|
60 | <div class="wrapper delegator"> |
---|
61 | <h3>This div has a delegate handler on it so clicking the blue DIV's below will have an effect.</h3> |
---|
62 | </div> |
---|
63 | <div class="wrapper delegator"> |
---|
64 | <h3>This div has a delegate handler on it so clicking the blue DIV's below will have an effect.</h3> |
---|
65 | </div> |
---|
66 | <div class="wrapper"> |
---|
67 | <h3>This div doesn't have a delegate handler on it so clicking the blue DIV's below will have no effect.</h3> |
---|
68 | </div> |
---|
69 | </div> |
---|
70 | </body> |
---|
71 | </html> |
---|
72 | |
---|