JavaScript中的HTMLCollection


本文作者: jsweibo

本文链接: https://jsweibo.github.io/2020/06/15/JavaScript%E4%B8%AD%E7%9A%84HTMLCollection/

摘要

本文主要讲述了:

  1. 什么是HTMLCollection
  2. 作用
  3. 实例属性
  4. 原型方法

正文

什么是HTMLCollection

一个类数组对象,原型为Object.prototype

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
// true
console.log(
Object.getPrototypeOf(HTMLCollection.prototype) === Object.prototype
);
</script>
</body>
</html>

作用

表示 HTML 元素集合,作为document.getElementsByClassName()document.getElementsByTagName()ParentNode.childrendocument.formsdocument.scriptsdocument.links等的返回值类型

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<p class="foo">hello, jsweibo</p>
<script>
console.log(document.getElementsByClassName('foo'));
console.log(document.getElementsByTagName('p'));
console.log(document.getElementsByTagName('p')[0].children);
console.log(document.forms);
console.log(document.scripts);
console.log(document.links);
</script>
</body>
</html>

注意:HTMLCollection是动态的,当 DOM 发生变更的时候,它也会自动更新

示例:动态的HTMLCollection

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<script>
const tempHTMLCollection = document.getElementsByClassName('foo');
console.log(tempHTMLCollection.length);
window.addEventListener('click', function () {
const foo = document.createElement('p');
foo.textContent = 'hello, jsweibo';
foo.className = 'foo';
document.body.appendChild(foo);
console.log(tempHTMLCollection.length);
});
</script>
</body>
</html>

实例属性

length

实例中 HTML 元素的个数

注意:该属性为可枚举属性,这意味着你不应该使用for...in遍历HTMLCollection实例的属性名称

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<script>
console.log(Object.getOwnPropertyDescriptors(HTMLCollection.prototype));
for (let key in HTMLCollection.prototype) {
console.log(key);
}
</script>
</body>
</html>

如果需要遍历HTMLCollection实例中的 HTML 元素,可以使用Array.prototype.forEachfor...of

示例:

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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<script>
const tempHTMLCollection = document.getElementsByClassName('foo');
Array.prototype.forEach.call(tempHTMLCollection, function (value) {
console.log(value);
});
for (let value of tempHTMLCollection) {
console.log(value);
}
</script>
</body>
</html>

原型方法

item

用于获取HTMLCollection实例中指定下标的元素,可以使用[]替代

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<script>
const tempHTMLCollection = document.getElementsByClassName('foo');
console.log(tempHTMLCollection.item(0));
</script>
</body>
</html>

注意:该属性为可枚举属性,这意味着你不应该使用for...in遍历HTMLCollection实例的属性名称

namedItem

用于获取HTMLCollection实例中指定nameid的元素

示例:获取name=test的元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<p class="foo">hello, jsweibo</p>
<p class="foo" name="test">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<script>
const tempHTMLCollection = document.getElementsByClassName('foo');
console.log(tempHTMLCollection.namedItem('test'));
</script>
</body>
</html>

示例:获取id=test的元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<p class="foo">hello, jsweibo</p>
<p class="foo" id="test">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<p class="foo">hello, jsweibo</p>
<script>
const tempHTMLCollection = document.getElementsByClassName('foo');
console.log(tempHTMLCollection.namedItem('test'));
</script>
</body>
</html>

注意:该属性为可枚举属性,这意味着你不应该使用for...in遍历HTMLCollection实例的属性名称

参考资料

本文作者: jsweibo

本文链接: https://jsweibo.github.io/2020/06/15/JavaScript%E4%B8%AD%E7%9A%84HTMLCollection/


本文对你有帮助?请支持我


支付宝
微信