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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
---
title: 使用 web 和 XML 開發來使用 DOM
slug: Web/API/Document_Object_Model/Examples
translation_of: Web/API/Document_Object_Model/Examples
---
<p>本章介紹了使用 DOM 進行 Web 以及 XML 開發的一些長範例。只要可能,在例子就會使用通用的 JavaScript Web API 、技巧以及模式來操作文檔對象(the document object)。</p>
<h2 id="Example_1:_height_and_width" name="Example_1:_height_and_width">範例一:高度和寬度</h2>
<p>下面的例子展示了在不同尺寸的圖片時使用其高(height)和寬(width)屬性的情況:</p>
<pre class="brush:html"><!DOCTYPE html>
<html lang="en">
<head>
<title>width/height example</title>
<script>
function init() {
var arrImages = new Array(3);
arrImages[0] = document.getElementById("image1");
arrImages[1] = document.getElementById("image2");
arrImages[2] = document.getElementById("image3");
var objOutput = document.getElementById("output");
var strHtml = "<ul>";
for (var i = 0; i < arrImages.length; i++) {
strHtml += "<li>image" + (i+1) +
": height=" + arrImages[i].height +
", width=" + arrImages[i].width +
", style.height=" + arrImages[i].style.height +
", style.width=" + arrImages[i].style.width +
"<\/li>";
}
strHtml += "<\/ul>";
objOutput.innerHTML = strHtml;
}
</script>
</head>
<body onload="init();">
<p>Image 1: no height, width, or style
<img id="image1" src="http://www.mozilla.org/images/mozilla-banner.gif">
</p>
<p>Image 2: height="50", width="500", but no style
<img id="image2"
src="http://www.mozilla.org/images/mozilla-banner.gif"
height="50" width="500">
</p>
<p>Image 3: no height, width, but style="height: 50px; width: 500px;"
<img id="image3"
src="http://www.mozilla.org/images/mozilla-banner.gif"
style="height: 50px; width: 500px;">
</p>
<div id="output"> </div>
</body>
</html>
</pre>
<h2 id="Example_2:_Image_Attributes" name="Example_2:_Image_Attributes">範例二:圖片屬性</h2>
<pre class="brush:html"><!DOCTYPE html>
<html lang="en">
<head>
<title>Modifying an image border</title>
<script>
function setBorderWidth(width) {
document.getElementById("img1").style.borderWidth = width + "px";
}
</script>
</head>
<body>
<p>
<img id="img1"
src="image1.gif"
style="border: 5px solid green;"
width="100" height="100" alt="border test">
</p>
<form name="FormName">
<input type="button" value="Make border 20px-wide" onclick="setBorderWidth(20);" />
<input type="button" value="Make border 5px-wide" onclick="setBorderWidth(5);" />
</form>
</body>
</html>
</pre>
<h2 id="Example_3:_Manipulating_Styles" name="Example_3:_Manipulating_Styles">範例三:改變樣式(style)</h2>
<p>在下面這個簡單的例子中,透過取得的 DOM 元素中的 style 物件和 style 物件中的屬性,我們可以取得 HTML 段落中的一些基本樣式屬性。在本例中,你可以直接操控各別的樣式屬性。在下一個的例子裡(見例4),你可以使用 stylesheets 和它的規則來改變整個文檔的樣式。</p>
<pre class="brush:html"><!DOCTYPE html>
<html lang="en">
<head>
<title>Changing color and font-size example</title>
<script>
function changeText() {
var p = document.getElementById("pid");
p.style.color = "blue"
p.style.fontSize = "18pt"
}
</script>
</head>
<body>
<p id="pid" onclick="window.location.href = 'http://www.cnn.com/';">linker</p>
<form>
<p><input value="rec" type="button" onclick="changeText();" /></p>
</form>
</body>
</html>
</pre>
<h2 id="Example_4:_Using_Stylesheets" name="Example_4:_Using_Stylesheets">範例四:使用樣式表(stylesheets)</h2>
<p>在文檔物件的 styleSheets 屬性會回傳一系列載入文檔中的樣式表(stylesheets)。你可以透過 styleSheets、style 和 CSSRule 物件來獲取樣式表和每條樣式規則。在下面的例子中,把所有的樣式規則中的選擇器文本(字符串)打印到控制台中。</p>
<pre class="brush:js">var ss = document.styleSheets;
for(var i = 0; i < ss.length; i++) {
for(var j = 0; j < ss[i].cssRules.length; j++) {
dump( ss[i].cssRules[j].selectorText + "\n" );
}
}</pre>
<p>下面是一個只定義了三條樣式規則的單一樣式表的文檔:</p>
<pre class="brush:css">body { background-color: darkblue; }
p { font-face: Arial; font-size: 10pt; margin-left: .125in; }
#lumpy { display: none; }
</pre>
<p>該腳本會輸出如下的結果:</p>
<pre>BODY
P
#LUMPY
</pre>
<h2 id="Example_5:_Event_Propagation" name="Example_5:_Event_Propagation">範例五:事件傳遞</h2>
<p>本例以一種簡單的方法闡述了事件是如何觸發以及在 DOM 中是如何處理的。當這個 HTML 文檔 BODY 載入的時候,在表格的首行註冊了一個事件監聽器。事件監聽器透過執行函數 stopEvent 來處理事件,從而改變在該表格底部的值。</p>
<p>然而,stopEvent 同時也會另外執行一個事件物件的方法{{domxref("event.stopPropagation")}},這會使得該事件(點擊)無法在 DOM 中有進一步的冒泡行為。請注意,表格本身有一個 {{domxref("element.onclick","onclick")}} 事件,使得這個表格被點擊時的時候原本應該要會跳出一個訊息。但因為 stopEvent 方法已經阻止了冒泡,所以在表格中的數據更新後,該事件階段有效地結束,表格的點擊事件沒有被觸發,而是顯示一個警告框(event propagation halted)——證實了事件被有效結束而沒有進一步冒泡。</p>
<pre class="brush:html"><!DOCTYPE html>
<html lang="en">
<head>
<title>Event Propagation</title>
<style>
#t-daddy { border: 1px solid red }
#c1 { background-color: pink; }
</style>
<script>
function stopEvent(ev) {
c2 = document.getElementById("c2");
c2.innerHTML = "hello";
// this ought to keep t-daddy from getting the click.
ev.stopPropagation();
alert("event propagation halted.");
}
function load() {
elem = document.getElementById("tbl1");
elem.addEventListener("click", stopEvent, false);
}
</script>
</head>
<body onload="load();">
<table id="t-daddy" onclick="alert('hi');">
<tr id="tbl1">
<td id="c1">one</td>
</tr>
<tr>
<td id="c2">two</td>
</tr>
</table>
</body>
</html>
</pre>
<h2 id="Example_6:_getComputedStyle" name="Example_6:_getComputedStyle">Example 6: getComputedStyle</h2>
<p>這個例子演示了如何用 {{domxref("window.getComputedStyle")}} 方法來獲取一個尚未被透過樣式元素或 JavaScript 設定的元素樣式(例如,elt.style.backgroundColor="RGB(173,216,230)")。列舉在 {{domxref("element.style", "elt.style")}} 後面的類型的樣式可以用更直接{{domxref("element.style", "elt.style")}} 屬性獲取。</p>
<p><code>getComputedStyle() 返回了一個 ComputedCSSStyleDeclaration 物件,其獨立的樣式屬性可以用該物件的 getPropertyValue() 方法引用,如同下面的例子一樣:</code></p>
<pre class="brush:html"><!DOCTYPE html>
<html lang="en">
<head>
<title>getComputedStyle example</title>
<script>
function cStyles() {
var RefDiv = document.getElementById("d1");
var txtHeight = document.getElementById("t1");
var h_style = document.defaultView.getComputedStyle(RefDiv, null).getPropertyValue("height");
txtHeight.value = h_style;
var txtWidth = document.getElementById("t2");
var w_style = document.defaultView.getComputedStyle(RefDiv, null).getPropertyValue("width");
txtWidth.value = w_style;
var txtBackgroundColor = document.getElementById("t3");
var b_style = document.defaultView.getComputedStyle(RefDiv, null).getPropertyValue("background-color");
txtBackgroundColor.value = b_style;
}
</script>
<style>
#d1 {
margin-left: 10px;
background-color: rgb(173, 216, 230);
height: 20px;
max-width: 20px;
}
</style>
</head>
<body>
<div id="d1">&nbsp;</div>
<form action="">
<p>
<button type="button" onclick="cStyles();">getComputedStyle</button>
height<input id="t1" type="text" value="1" />
max-width<input id="t2" type="text" value="2" />
bg-color<input id="t3" type="text" value="3" />
</p>
</form>
</body>
</html>
</pre>
<h2 id="Example_7:_Displaying_Event_Object_Properties" name="Example_7:_Displaying_Event_Object_Properties">範例七:顯示事件物件的屬性</h2>
<p>這個例子使用 DOM 方法來顯示所有 {{domxref("window.onload")}} {{domxref("event")}} 物件的屬性及其在 table 中的值。這個方法也展示一個有用的技術,使用 for...in 迴圈來來遍歷一個物件的屬性,以得到它們的值。</p>
<p>不同瀏覽器之間事件物件的屬性有很大不同,<a href="https://dom.spec.whatwg.org">WHATWG DOM Standard</a> 規範了事件的標準屬性,然而,許多瀏覽器都大大擴展了這些。</p>
<p>將下面的代碼放到一個空白的文本文件,並將其用各種瀏覽器開啟,你一定會對各種瀏覽器之間的不一致(事件屬性的名稱及其數量)感到驚訝。你可能還喜歡在這個頁面加入一些元素,並呼叫不同的事件處理函數(event handlers)。</p>
<pre class="brush:html"><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Show Event properties</title>
<style>
table { border-collapse: collapse; }
thead { font-weight: bold; }
td { padding: 2px 10px 2px 10px; }
.odd { background-color: #efdfef; }
.even { background-color: #ffffff; }
</style>
<script>
function showEventProperties(e) {
function addCell(row, text) {
var cell = row.insertCell(-1);
cell.appendChild(document.createTextNode(text));
}
var e = e || window.event;
document.getElementById('eventType').innerHTML = e.type;
var table = document.createElement('table');
var thead = table.createTHead();
var row = thead.insertRow(-1);
var lableList = ['#', 'Property', 'Value'];
var len = lableList.length;
for (var i=0; i<len; i++) {
addCell(row, lableList[i]);
}
var tbody = document.createElement('tbody');
table.appendChild(tbody);
for (var p in e) {
row = tbody.insertRow(-1);
row.className = (row.rowIndex % 2)? 'odd':'even';
addCell(row, row.rowIndex);
addCell(row, p);
addCell(row, e[p]);
}
document.body.appendChild(table);
}
window.onload = function(event){
showEventProperties(event);
}
</script>
</head>
<body>
<h1>Properties of the DOM <span id="eventType"></span> Event Object</h1>
</body>
</html>
</pre>
<h2 id="Example_8:_Using_the_DOM_Table_Interface" name="Example_8:_Using_the_DOM_Table_Interface">範例八:使用 DOM Table 介面</h2>
<p>DOM HTMLTableElement 介面提供了一些方便的方法用於創建和操作資料表。兩種常用的方法是{{domxref("HTMLTableElement.insertRow")}}和{{domxref("tableRow.insertCell")}}.。</p>
<p>增加一行和一些細格到現有的資料表:</p>
<pre class="brush:html"><table id="table0">
<tr>
<td>Row 0 Cell 0</td>
<td>Row 0 Cell 1</td>
</tr>
</table>
<script>
var table = document.getElementById('table0');
var row = table.insertRow(-1);
var cell,
text;
for (var i = 0; i < 2; i++) {
cell = row.insertCell(-1);
text = 'Row ' + row.rowIndex + ' Cell ' + i;
cell.appendChild(document.createTextNode(text));
}
</script>
</pre>
<h3 id="Notes" name="Notes">提醒</h3>
<ul>
<li>表格的{{domxref("element.innerHTML","innerHTML")}}屬性絕不應該被用來修改表,雖然你可以用它來寫一個完整的表格或細格中的內容。</li>
<li>如果用 DOM 核心方法<font face="Consolas, Liberation Mono, Courier, monospace"> </font>{{domxref("document.createElement")}} 和<font face="Consolas, Liberation Mono, Courier, monospace"> </font>{{domxref("Node.appendChild")}} 來建立表格的行和細格,IE 會要求它們附加到一個 tbody 元素,而其它瀏覽器允許它們附加到一個 table 元素(行會被添加到最後的 tbody 元素)。</li>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableElement#Methods">表格介面</a>還有一些可用於創建和修改的表格的便利方法。</li>
</ul>
<h2 id="Subnav">Subnav</h2>
<ul>
<li><a href="/en-US/docs/Web/API/Document_Object_Model">DOM Reference</a></li>
<li><a href="/en-US/docs/Web/API/Document_Object_Model/Introduction">Introduction to the DOM</a></li>
<li><a href="/en-US/docs/Web/API/Document_Object_Model/Events">Events and the DOM</a></li>
<li><a href="/en-US/docs/Web/API/Document_Object_Model/Examples">Examples</a></li>
</ul>
|