回答編集履歴
4
a
test
CHANGED
@@ -129,3 +129,15 @@
|
|
129
129
|
|
130
130
|
|
131
131
|
- [querySelectorAll](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll)
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
# デバッグについて
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+

|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
`console.log`を活用したり、Chrome Developer Toolやエディターのデバッグ機能を活用して、気になるところの値を確認すると、上手くいっていない部分の特定がしやすいと思います。[https://developers.google.com/web/tools/chrome-devtools/javascript/?hl=ja](https://developers.google.com/web/tools/chrome-devtools/javascript/?hl=ja)
|
3
a
test
CHANGED
@@ -46,6 +46,78 @@
|
|
46
46
|
|
47
47
|
|
48
48
|
|
49
|
+
# 上手く行かないパターンの説明
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
```
|
54
|
+
|
55
|
+
var elements = document.getElementsByClassName('test');
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
var i = 0;
|
60
|
+
|
61
|
+
for (i; i <= elements.length; i++) {
|
62
|
+
|
63
|
+
elements[i].className = "demo";
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
```
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
`document.getElementsByClassName()`は、引数に与えたクラス名にマッチしたDOMノードを,
|
72
|
+
|
73
|
+
`HTMLCollection`インターフェイスを持つ配列のようなリストに入れて返してくれます。
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
`HTMLCollection`の説明を[MDN](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection)で確認すると、以下のような説明があります。
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
> An HTMLCollection in the HTML DOM is live; it is automatically updated when the underlying document is changed.
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
ここに書いてある通り、HTMLCollectionはそこに含まれる内容に変更があった場合、自動で最新の状態に更新されてしまいます。
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
```
|
90
|
+
|
91
|
+
var elements = document.getElementsByClassName('test');
|
92
|
+
|
93
|
+
console.log(elements.length) //3
|
94
|
+
|
95
|
+
elements[0].className = "demo";
|
96
|
+
|
97
|
+
console.log(elements.length); //2
|
98
|
+
|
99
|
+
```
|
100
|
+
|
101
|
+
なので、このようにHTMLCollectionに含まれるDOMノードのクラス名を変えてしまうと、クラス名が変更したことによって、そのDOMノードはHTMLCollectionから除外され、リストのlengthも変わってしまいます。
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
これによって、for loopでクラス名を変更させようとした場合に、lengthの値が変わってしまうため、上手く帳尻が合わなくなってしまいます。
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
> The Element method querySelectorAll() returns a static (not live) NodeList representing a list of the document's elements that match the specified group of selectors. ([MDN](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll))
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
一方、`document.querySelectorAll()`は、`HTMLCollection`ではなく、`NodeList`を返してくれて、`NodeList`は`HTMLCollection`とは異なり、静的なものなので、より直感的に扱うことが出来ます。
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
|
120
|
+
|
49
121
|
# 参考
|
50
122
|
|
51
123
|
- [getElementsByClassName](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName)
|
2
atode
test
CHANGED
@@ -43,3 +43,17 @@
|
|
43
43
|
})
|
44
44
|
|
45
45
|
```
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
# 参考
|
50
|
+
|
51
|
+
- [getElementsByClassName](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName)
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
- [HTMLCollection](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection)
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
- [querySelectorAll](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll)
|
1
a
test
CHANGED
@@ -1,3 +1,35 @@
|
|
1
|
+
```HTML
|
2
|
+
|
3
|
+
<p class='test'>test<p>
|
4
|
+
|
5
|
+
<p class='test'>test<p>
|
6
|
+
|
7
|
+
<p class='test'>test<p>
|
8
|
+
|
9
|
+
```
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
```CSS
|
14
|
+
|
15
|
+
.test {
|
16
|
+
|
17
|
+
font-size: 12px;
|
18
|
+
|
19
|
+
}
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
.demo {
|
24
|
+
|
25
|
+
font-size: 24px;
|
26
|
+
|
27
|
+
}
|
28
|
+
|
29
|
+
```
|
30
|
+
|
31
|
+
|
32
|
+
|
1
33
|
```JavaScript
|
2
34
|
|
3
35
|
var elements = document.querySelectorAll('.test');
|