|
525
|
1 |
<!DOCTYPE html> |
|
|
2 |
<html lang="en"> |
|
|
3 |
<head> |
|
|
4 |
<meta charset="utf-8"> |
|
|
5 |
<title>Example: Set and Get Properties</title> |
|
|
6 |
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700,400italic,700italic"> |
|
|
7 |
<link rel="stylesheet" href="../../build/cssgrids/cssgrids-min.css"> |
|
|
8 |
<link rel="stylesheet" href="../assets/css/main.css"> |
|
|
9 |
<link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css"> |
|
|
10 |
<link rel="shortcut icon" type="image/png" href="../assets/favicon.png"> |
|
|
11 |
<script src="../../build/yui/yui-min.js"></script> |
|
|
12 |
|
|
|
13 |
</head> |
|
|
14 |
<body> |
|
|
15 |
<!-- |
|
|
16 |
<a href="https://github.com/yui/yui3"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub"></a> |
|
|
17 |
--> |
|
|
18 |
<div id="doc"> |
|
|
19 |
<div id="hd"> |
|
|
20 |
<h1><img src="http://yuilibrary.com/img/yui-logo.png"></h1> |
|
|
21 |
</div> |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
<h1>Example: Set and Get Properties</h1> |
|
|
25 |
<div class="yui3-g"> |
|
|
26 |
<div class="yui3-u-3-4"> |
|
|
27 |
<div id="main"> |
|
|
28 |
<div class="content"><link href="../assets/node/node.css" rel="stylesheet" type="text/css"> |
|
|
29 |
<div class="intro"> |
|
|
30 |
<p>This example demonstrates how to retrieve and use a <code>Node</code> instance |
|
|
31 |
and access DOM properties.</p> |
|
|
32 |
<p>Press a button to get or set the <code>offsetWidth</code> of the <code>div</code> |
|
|
33 |
containing the corn image.</p> |
|
|
34 |
</div> |
|
|
35 |
|
|
|
36 |
<div class="example"> |
|
|
37 |
<style> |
|
|
38 |
.example #corn{ |
|
|
39 |
position: relative; |
|
|
40 |
background: url(../assets/node/images/corn.jpg); |
|
|
41 |
width: 232px; |
|
|
42 |
height: 181px; |
|
|
43 |
-moz-box-shadow: 3px 3px 12px rgba(0, 0, 0, 0.4); |
|
|
44 |
-webkit-box-shadow: 3px 3px 12px rgba(0, 0, 0, 0.4); |
|
|
45 |
margin: 2px 0 1em; |
|
|
46 |
border: none; |
|
|
47 |
} |
|
|
48 |
.example #ruler{ |
|
|
49 |
width: 650px; |
|
|
50 |
height: 42px; |
|
|
51 |
background: url("../assets/node/images/ruler_ticks.png") repeat-x scroll -1px 24px #DDCEB7; |
|
|
52 |
} |
|
|
53 |
.example .yui3-button { |
|
|
54 |
margin: 0 0.2em; |
|
|
55 |
} |
|
|
56 |
.example .btn-get{ |
|
|
57 |
margin-left: 4em; |
|
|
58 |
} |
|
|
59 |
#input { |
|
|
60 |
height: 1.6em; |
|
|
61 |
width: 4em; |
|
|
62 |
} |
|
|
63 |
#output{ |
|
|
64 |
position: absolute; |
|
|
65 |
top: -40px; |
|
|
66 |
width: 100px; |
|
|
67 |
height: 40px; |
|
|
68 |
right: -50px; |
|
|
69 |
text-align: center; |
|
|
70 |
cursor: pointer; |
|
|
71 |
} |
|
|
72 |
#corn .ruler-marker{ |
|
|
73 |
position: absolute; |
|
|
74 |
top: -20px; |
|
|
75 |
right: 0; |
|
|
76 |
height: 30px; |
|
|
77 |
border-right: solid 1px #f00; |
|
|
78 |
} |
|
|
79 |
</style> |
|
|
80 |
|
|
|
81 |
<body> |
|
|
82 |
<div id="ruler"></div> |
|
|
83 |
<div id="corn"> |
|
|
84 |
<div class="ruler-marker"></div> |
|
|
85 |
<div id="output">? px</div> |
|
|
86 |
</div> |
|
|
87 |
<label>Width:</label> |
|
|
88 |
<input id="input" size="2" value="550"> px |
|
|
89 |
<button class="yui3-button btn-set">Set</button> |
|
|
90 |
<button class="yui3-button btn-get">Get</button> |
|
|
91 |
<script> |
|
|
92 |
YUI().use('node', 'button', function(Y) { |
|
|
93 |
var corn = Y.one('#corn'), |
|
|
94 |
input = Y.one('.example #input'), |
|
|
95 |
output = Y.one('.example #output'); |
|
|
96 |
|
|
|
97 |
var getWidth = function(){ |
|
|
98 |
var width = corn.get('offsetWidth'); |
|
|
99 |
output.setHTML(width + 'px'); // display width near the get button |
|
|
100 |
} |
|
|
101 |
|
|
|
102 |
Y.one('.example .btn-get').on('click', getWidth); |
|
|
103 |
output.on('click', getWidth); // also allows getting width by clicking on ruler width label |
|
|
104 |
|
|
|
105 |
Y.one('.example .btn-set').on('click', function(e) { |
|
|
106 |
var value = input.get('value'), |
|
|
107 |
width = corn.get('offsetWidth'); |
|
|
108 |
if (value == '') { |
|
|
109 |
input.set('value', width); |
|
|
110 |
} else if (!isNaN(parseInt(value))) { // if the value in the input is a number |
|
|
111 |
corn.set('offsetWidth', value); |
|
|
112 |
output.setHTML('? ' + 'px'); // clear out the width label on the ruler |
|
|
113 |
} |
|
|
114 |
}); |
|
|
115 |
}); |
|
|
116 |
</script> |
|
|
117 |
|
|
|
118 |
<div style="color:#aaa; display:block; font-size:90%; margin-top:1em;">Image derived from |
|
|
119 |
<a href="http://www.flickr.com/photos/29278394@N00/3934433763/" target="_blank"> |
|
|
120 |
"ear of corn"</a>, by normanack</div> |
|
|
121 |
</div> |
|
|
122 |
|
|
|
123 |
<h2>Setting up the HTML</h2> |
|
|
124 |
|
|
|
125 |
<pre class="code prettyprint"><div id="ruler"></div> |
|
|
126 |
<div id="corn"> |
|
|
127 |
<div class="ruler-marker"></div> |
|
|
128 |
<div id="output">? px</div> |
|
|
129 |
</div> |
|
|
130 |
<label>Width:</label> |
|
|
131 |
<input id="input" size="2" value="550"> px |
|
|
132 |
<button class="yui3-button btn-set">Set</button> |
|
|
133 |
<button class="yui3-button btn-get">Get</button></pre> |
|
|
134 |
|
|
|
135 |
|
|
|
136 |
<h2>Getting a Node Instance</h2> |
|
|
137 |
<p>The <code>Y.one</code> method accepts a Selector or HTML element and |
|
|
138 |
returns a Node instance. First we'll set up some variables |
|
|
139 |
for the node's representing our HTML.</p> |
|
|
140 |
|
|
|
141 |
<pre class="code prettyprint">var corn = Y.one('#corn'), |
|
|
142 |
input = Y.one('.example #input'), |
|
|
143 |
output = Y.one('.example #output');</pre> |
|
|
144 |
|
|
|
145 |
|
|
|
146 |
<h2>Accessing Node Properties</h2> |
|
|
147 |
<p>The properties of a node can be accessed via its <code>set</code> and |
|
|
148 |
<code>get</code> methods.</p> |
|
|
149 |
<p>In most cases, simple type of properties (strings, numbers, Booleans) |
|
|
150 |
pass directly to/from the underlying DOM node, however properties representing |
|
|
151 |
other DOM nodes return <code>Node</code> or <code>NodeList</code> instances.</p> |
|
|
152 |
|
|
|
153 |
<h3>The Get Method</h3> |
|
|
154 |
<p>We can use the <code>get</code> method to read the <code>offsetWidth</code> of the <code>div</code> |
|
|
155 |
containing the corn image, |
|
|
156 |
which includes the style width, padding, and border.</p> |
|
|
157 |
|
|
|
158 |
<pre class="code prettyprint">width = corn.get('offsetWidth');</pre> |
|
|
159 |
|
|
|
160 |
|
|
|
161 |
<h3>The Set Method</h3> |
|
|
162 |
<p>The Set method can be used to set the value of objects |
|
|
163 |
</p> |
|
|
164 |
<pre class="code prettyprint">input.set('value', 237);</pre> |
|
|
165 |
|
|
|
166 |
|
|
|
167 |
<p>The <code>offsetWidth</code> property of an HTML element is read only, however YUI |
|
|
168 |
makes this writeable. This assures that the final <code>offsetWidth</code> |
|
|
169 |
matches the value that is set, regardless of borders, padding, or box model.</p> |
|
|
170 |
|
|
|
171 |
<pre class="code prettyprint">corn.set('offsetWidth', value);</pre> |
|
|
172 |
|
|
|
173 |
|
|
|
174 |
<h2>Listening for Node Events</h2> |
|
|
175 |
<p>We will update the value <code>offsetWidth</code> property of the <code>div</code> |
|
|
176 |
containing the corn image when the Set button is pressed. |
|
|
177 |
</p> |
|
|
178 |
|
|
|
179 |
<pre class="code prettyprint">Y.one('.example .btn-set').on('click', function(e) { |
|
|
180 |
... |
|
|
181 |
corn.set('offsetWidth', value); |
|
|
182 |
... |
|
|
183 |
};</pre> |
|
|
184 |
|
|
|
185 |
|
|
|
186 |
<h2>Complete Example Source</h2> |
|
|
187 |
<pre class="code prettyprint"><style> |
|
|
188 |
.example #corn{ |
|
|
189 |
position: relative; |
|
|
190 |
background: url(../assets/node/images/corn.jpg); |
|
|
191 |
width: 232px; |
|
|
192 |
height: 181px; |
|
|
193 |
-moz-box-shadow: 3px 3px 12px rgba(0, 0, 0, 0.4); |
|
|
194 |
-webkit-box-shadow: 3px 3px 12px rgba(0, 0, 0, 0.4); |
|
|
195 |
margin: 2px 0 1em; |
|
|
196 |
border: none; |
|
|
197 |
} |
|
|
198 |
.example #ruler{ |
|
|
199 |
width: 650px; |
|
|
200 |
height: 42px; |
|
|
201 |
background: url("../assets/node/images/ruler_ticks.png") repeat-x scroll -1px 24px #DDCEB7; |
|
|
202 |
} |
|
|
203 |
.example .yui3-button { |
|
|
204 |
margin: 0 0.2em; |
|
|
205 |
} |
|
|
206 |
.example .btn-get{ |
|
|
207 |
margin-left: 4em; |
|
|
208 |
} |
|
|
209 |
#input { |
|
|
210 |
height: 1.6em; |
|
|
211 |
width: 4em; |
|
|
212 |
} |
|
|
213 |
#output{ |
|
|
214 |
position: absolute; |
|
|
215 |
top: -40px; |
|
|
216 |
width: 100px; |
|
|
217 |
height: 40px; |
|
|
218 |
right: -50px; |
|
|
219 |
text-align: center; |
|
|
220 |
cursor: pointer; |
|
|
221 |
} |
|
|
222 |
#corn .ruler-marker{ |
|
|
223 |
position: absolute; |
|
|
224 |
top: -20px; |
|
|
225 |
right: 0; |
|
|
226 |
height: 30px; |
|
|
227 |
border-right: solid 1px #f00; |
|
|
228 |
} |
|
|
229 |
</style> |
|
|
230 |
|
|
|
231 |
<body> |
|
|
232 |
<div id="ruler"></div> |
|
|
233 |
<div id="corn"> |
|
|
234 |
<div class="ruler-marker"></div> |
|
|
235 |
<div id="output">? px</div> |
|
|
236 |
</div> |
|
|
237 |
<label>Width:</label> |
|
|
238 |
<input id="input" size="2" value="550"> px |
|
|
239 |
<button class="yui3-button btn-set">Set</button> |
|
|
240 |
<button class="yui3-button btn-get">Get</button> |
|
|
241 |
<script> |
|
|
242 |
YUI().use('node', 'button', function(Y) { |
|
|
243 |
var corn = Y.one('#corn'), |
|
|
244 |
input = Y.one('.example #input'), |
|
|
245 |
output = Y.one('.example #output'); |
|
|
246 |
|
|
|
247 |
var getWidth = function(){ |
|
|
248 |
var width = corn.get('offsetWidth'); |
|
|
249 |
output.setHTML(width + 'px'); // display width near the get button |
|
|
250 |
} |
|
|
251 |
|
|
|
252 |
Y.one('.example .btn-get').on('click', getWidth); |
|
|
253 |
output.on('click', getWidth); // also allows getting width by clicking on ruler width label |
|
|
254 |
|
|
|
255 |
Y.one('.example .btn-set').on('click', function(e) { |
|
|
256 |
var value = input.get('value'), |
|
|
257 |
width = corn.get('offsetWidth'); |
|
|
258 |
if (value == '') { |
|
|
259 |
input.set('value', width); |
|
|
260 |
} else if (!isNaN(parseInt(value))) { // if the value in the input is a number |
|
|
261 |
corn.set('offsetWidth', value); |
|
|
262 |
output.setHTML('? ' + 'px'); // clear out the width label on the ruler |
|
|
263 |
} |
|
|
264 |
}); |
|
|
265 |
}); |
|
|
266 |
</script></pre> |
|
|
267 |
|
|
|
268 |
</div> |
|
|
269 |
</div> |
|
|
270 |
</div> |
|
|
271 |
|
|
|
272 |
<div class="yui3-u-1-4"> |
|
|
273 |
<div class="sidebar"> |
|
|
274 |
|
|
|
275 |
|
|
|
276 |
|
|
|
277 |
<div class="sidebox"> |
|
|
278 |
<div class="hd"> |
|
|
279 |
<h2 class="no-toc">Examples</h2> |
|
|
280 |
</div> |
|
|
281 |
|
|
|
282 |
<div class="bd"> |
|
|
283 |
<ul class="examples"> |
|
|
284 |
|
|
|
285 |
|
|
|
286 |
<li data-description="Using selectors and property accessors with Node."> |
|
|
287 |
<a href="properties.html">Set and Get Properties</a> |
|
|
288 |
</li> |
|
|
289 |
|
|
|
290 |
|
|
|
291 |
|
|
|
292 |
<li data-description="Using DOM methods with Node."> |
|
|
293 |
<a href="dom-node.html">DOM Methods</a> |
|
|
294 |
</li> |
|
|
295 |
|
|
|
296 |
|
|
|
297 |
|
|
|
298 |
<li data-description="Building a simple store and shopping cart."> |
|
|
299 |
<a href="store.html">DOM Methods - Store</a> |
|
|
300 |
</li> |
|
|
301 |
|
|
|
302 |
|
|
|
303 |
|
|
|
304 |
<li data-description="Listening for DOM events with Node instances."> |
|
|
305 |
<a href="events.html">Handling DOM Events</a> |
|
|
306 |
</li> |
|
|
307 |
|
|
|
308 |
|
|
|
309 |
|
|
|
310 |
<li data-description="NodeList provides Node functionality for manipulating multiple nodes at once."> |
|
|
311 |
<a href="nodelist.html">Using NodeList - Simple</a> |
|
|
312 |
</li> |
|
|
313 |
|
|
|
314 |
|
|
|
315 |
|
|
|
316 |
<li data-description="How to use multiple NodeList features to build a simple game."> |
|
|
317 |
<a href="ducks.html">Using NodeList - Ducks Game</a> |
|
|
318 |
</li> |
|
|
319 |
|
|
|
320 |
|
|
|
321 |
|
|
|
322 |
<li data-description="Using a single event listener to handle events on multiple nodes."> |
|
|
323 |
<a href="node-evt-delegation.html">Delegating Node Events</a> |
|
|
324 |
</li> |
|
|
325 |
|
|
|
326 |
|
|
|
327 |
|
|
|
328 |
<li data-description="This example demonstrates how to position an element in page coordinates."> |
|
|
329 |
<a href="node-xy.html">Node Positioning</a> |
|
|
330 |
</li> |
|
|
331 |
|
|
|
332 |
|
|
|
333 |
|
|
|
334 |
<li data-description="This example demonstrates how to set styles and get style information."> |
|
|
335 |
<a href="node-style.html">Node Styling</a> |
|
|
336 |
</li> |
|
|
337 |
|
|
|
338 |
|
|
|
339 |
|
|
|
340 |
<li data-description="This example demonstrates how to insert content into a Node."> |
|
|
341 |
<a href="node-insert.html">Adding Node Content - Burger Builder</a> |
|
|
342 |
</li> |
|
|
343 |
|
|
|
344 |
|
|
|
345 |
|
|
|
346 |
<li data-description="This example demonstrates how to show and hide a Node."> |
|
|
347 |
<a href="node-view.html">Showing and Hiding</a> |
|
|
348 |
</li> |
|
|
349 |
|
|
|
350 |
|
|
|
351 |
|
|
|
352 |
|
|
|
353 |
|
|
|
354 |
|
|
|
355 |
|
|
|
356 |
|
|
|
357 |
|
|
|
358 |
|
|
|
359 |
|
|
|
360 |
|
|
|
361 |
|
|
|
362 |
|
|
|
363 |
|
|
|
364 |
|
|
|
365 |
</ul> |
|
|
366 |
</div> |
|
|
367 |
</div> |
|
|
368 |
|
|
|
369 |
|
|
|
370 |
|
|
|
371 |
<div class="sidebox"> |
|
|
372 |
<div class="hd"> |
|
|
373 |
<h2 class="no-toc">Examples That Use This Component</h2> |
|
|
374 |
</div> |
|
|
375 |
|
|
|
376 |
<div class="bd"> |
|
|
377 |
<ul class="examples"> |
|
|
378 |
|
|
|
379 |
|
|
|
380 |
|
|
|
381 |
|
|
|
382 |
|
|
|
383 |
|
|
|
384 |
|
|
|
385 |
|
|
|
386 |
|
|
|
387 |
|
|
|
388 |
|
|
|
389 |
|
|
|
390 |
|
|
|
391 |
|
|
|
392 |
|
|
|
393 |
|
|
|
394 |
|
|
|
395 |
|
|
|
396 |
|
|
|
397 |
|
|
|
398 |
|
|
|
399 |
|
|
|
400 |
|
|
|
401 |
|
|
|
402 |
<li data-description="Creating an accessible toolbar using the Focus Manager Node Plugin and Node's support for the WAI-ARIA Roles and States."> |
|
|
403 |
<a href="../node-focusmanager/node-focusmanager-toolbar.html">Accessible Toolbar</a> |
|
|
404 |
</li> |
|
|
405 |
|
|
|
406 |
|
|
|
407 |
|
|
|
408 |
<li data-description="Creating an accessible menu button using the Focus Manager Node Plugin, Event's delegation support and mouseenter event, along with the Overlay widget and Node's support for the WAI-ARIA Roles and States."> |
|
|
409 |
<a href="../node-focusmanager/node-focusmanager-button.html">Accessible Menu Button</a> |
|
|
410 |
</li> |
|
|
411 |
|
|
|
412 |
|
|
|
413 |
|
|
|
414 |
<li data-description="Use the Event Utility to attach simple DOM event handlers."> |
|
|
415 |
<a href="../event/basic-example.html">Simple DOM Events</a> |
|
|
416 |
</li> |
|
|
417 |
|
|
|
418 |
|
|
|
419 |
|
|
|
420 |
<li data-description="Example Photo Browser application."> |
|
|
421 |
<a href="../dd/photo-browser.html">Photo Browser</a> |
|
|
422 |
</li> |
|
|
423 |
|
|
|
424 |
|
|
|
425 |
|
|
|
426 |
<li data-description="Portal style example using Drag & Drop Event Bubbling and Animation."> |
|
|
427 |
<a href="../dd/portal-drag.html">Portal Style Example</a> |
|
|
428 |
</li> |
|
|
429 |
|
|
|
430 |
|
|
|
431 |
|
|
|
432 |
<li data-description="Use IO to request XML data from a remote web service."> |
|
|
433 |
<a href="../io/weather.html">Request XML data from Yahoo! Weather</a> |
|
|
434 |
</li> |
|
|
435 |
|
|
|
436 |
|
|
|
437 |
|
|
|
438 |
<li data-description="Use IO to make a cross-domain request to Yahoo! Pipes, returning data from disparate sources."> |
|
|
439 |
<a href="../io/xdr.html">Request JSON using Yahoo! Pipes</a> |
|
|
440 |
</li> |
|
|
441 |
|
|
|
442 |
|
|
|
443 |
</ul> |
|
|
444 |
</div> |
|
|
445 |
</div> |
|
|
446 |
|
|
|
447 |
</div> |
|
|
448 |
</div> |
|
|
449 |
</div> |
|
|
450 |
</div> |
|
|
451 |
|
|
|
452 |
<script src="../assets/vendor/prettify/prettify-min.js"></script> |
|
|
453 |
<script>prettyPrint();</script> |
|
|
454 |
|
|
|
455 |
<script> |
|
|
456 |
YUI.Env.Tests = { |
|
|
457 |
examples: [], |
|
|
458 |
project: '../assets', |
|
|
459 |
assets: '../assets/node', |
|
|
460 |
name: 'properties', |
|
|
461 |
title: 'Set and Get Properties', |
|
|
462 |
newWindow: '', |
|
|
463 |
auto: false |
|
|
464 |
}; |
|
|
465 |
YUI.Env.Tests.examples.push('properties'); |
|
|
466 |
YUI.Env.Tests.examples.push('dom-node'); |
|
|
467 |
YUI.Env.Tests.examples.push('store'); |
|
|
468 |
YUI.Env.Tests.examples.push('events'); |
|
|
469 |
YUI.Env.Tests.examples.push('nodelist'); |
|
|
470 |
YUI.Env.Tests.examples.push('ducks'); |
|
|
471 |
YUI.Env.Tests.examples.push('node-evt-delegation'); |
|
|
472 |
YUI.Env.Tests.examples.push('node-xy'); |
|
|
473 |
YUI.Env.Tests.examples.push('node-style'); |
|
|
474 |
YUI.Env.Tests.examples.push('node-insert'); |
|
|
475 |
YUI.Env.Tests.examples.push('node-view'); |
|
|
476 |
YUI.Env.Tests.examples.push('node-focusmanager-toolbar'); |
|
|
477 |
YUI.Env.Tests.examples.push('node-focusmanager-button'); |
|
|
478 |
YUI.Env.Tests.examples.push('basic-example'); |
|
|
479 |
YUI.Env.Tests.examples.push('photo-browser'); |
|
|
480 |
YUI.Env.Tests.examples.push('portal-drag'); |
|
|
481 |
YUI.Env.Tests.examples.push('weather'); |
|
|
482 |
YUI.Env.Tests.examples.push('xdr'); |
|
|
483 |
|
|
|
484 |
</script> |
|
|
485 |
<script src="../assets/yui/test-runner.js"></script> |
|
|
486 |
|
|
|
487 |
|
|
|
488 |
|
|
|
489 |
</body> |
|
|
490 |
</html> |