代码示例:(标识:jsck_htmlcollection_2)
<!DOCTYPE html>
<html>
<body>

<h1>HTMLCollection 对象</h1>

<p>使用 getElementsByTagName() 方法来返回 HTMLCollection。</p>

<p>此页面上 P 元素的数量:</p>

<script>
var x = document.getElementsByTagName("P");
document.write(x.length);
</script>

</body>
</html>
运行结果: