回答編集履歴
4
a
answer
CHANGED
@@ -63,4 +63,10 @@
|
|
63
63
|
|
64
64
|
- [HTMLCollection](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection)
|
65
65
|
|
66
|
-
- [querySelectorAll](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll)
|
66
|
+
- [querySelectorAll](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll)
|
67
|
+
|
68
|
+
# デバッグについて
|
69
|
+
|
70
|
+

|
71
|
+
|
72
|
+
`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
answer
CHANGED
@@ -22,6 +22,42 @@
|
|
22
22
|
})
|
23
23
|
```
|
24
24
|
|
25
|
+
# 上手く行かないパターンの説明
|
26
|
+
|
27
|
+
```
|
28
|
+
var elements = document.getElementsByClassName('test');
|
29
|
+
|
30
|
+
var i = 0;
|
31
|
+
for (i; i <= elements.length; i++) {
|
32
|
+
elements[i].className = "demo";
|
33
|
+
}
|
34
|
+
```
|
35
|
+
|
36
|
+
`document.getElementsByClassName()`は、引数に与えたクラス名にマッチしたDOMノードを,
|
37
|
+
`HTMLCollection`インターフェイスを持つ配列のようなリストに入れて返してくれます。
|
38
|
+
|
39
|
+
`HTMLCollection`の説明を[MDN](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection)で確認すると、以下のような説明があります。
|
40
|
+
|
41
|
+
> An HTMLCollection in the HTML DOM is live; it is automatically updated when the underlying document is changed.
|
42
|
+
|
43
|
+
ここに書いてある通り、HTMLCollectionはそこに含まれる内容に変更があった場合、自動で最新の状態に更新されてしまいます。
|
44
|
+
|
45
|
+
```
|
46
|
+
var elements = document.getElementsByClassName('test');
|
47
|
+
console.log(elements.length) //3
|
48
|
+
elements[0].className = "demo";
|
49
|
+
console.log(elements.length); //2
|
50
|
+
```
|
51
|
+
なので、このようにHTMLCollectionに含まれるDOMノードのクラス名を変えてしまうと、クラス名が変更したことによって、そのDOMノードはHTMLCollectionから除外され、リストのlengthも変わってしまいます。
|
52
|
+
|
53
|
+
これによって、for loopでクラス名を変更させようとした場合に、lengthの値が変わってしまうため、上手く帳尻が合わなくなってしまいます。
|
54
|
+
|
55
|
+
> 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))
|
56
|
+
|
57
|
+
一方、`document.querySelectorAll()`は、`HTMLCollection`ではなく、`NodeList`を返してくれて、`NodeList`は`HTMLCollection`とは異なり、静的なものなので、より直感的に扱うことが出来ます。
|
58
|
+
|
59
|
+
|
60
|
+
|
25
61
|
# 参考
|
26
62
|
- [getElementsByClassName](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName)
|
27
63
|
|
2
atode
answer
CHANGED
@@ -20,4 +20,11 @@
|
|
20
20
|
elements.forEach(function(element) {
|
21
21
|
element.className = "demo";
|
22
22
|
})
|
23
|
-
```
|
23
|
+
```
|
24
|
+
|
25
|
+
# 参考
|
26
|
+
- [getElementsByClassName](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName)
|
27
|
+
|
28
|
+
- [HTMLCollection](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection)
|
29
|
+
|
30
|
+
- [querySelectorAll](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll)
|
1
a
answer
CHANGED
@@ -1,3 +1,19 @@
|
|
1
|
+
```HTML
|
2
|
+
<p class='test'>test<p>
|
3
|
+
<p class='test'>test<p>
|
4
|
+
<p class='test'>test<p>
|
5
|
+
```
|
6
|
+
|
7
|
+
```CSS
|
8
|
+
.test {
|
9
|
+
font-size: 12px;
|
10
|
+
}
|
11
|
+
|
12
|
+
.demo {
|
13
|
+
font-size: 24px;
|
14
|
+
}
|
15
|
+
```
|
16
|
+
|
1
17
|
```JavaScript
|
2
18
|
var elements = document.querySelectorAll('.test');
|
3
19
|
|