JavaScript HTML DOM 集合(Collection)
本章节介绍如何向文档中添加和移除元素(节点)。
HTMLCollection 对象
getElementsByTagName() 方法返回 HTMLCollection 对象。
HTMLCollection 对象类似包含 HTML 元素的一个数组。
以下代码获取文档所有的 <p> 元素:
实例
集合中的元素可以通过索引(以 0 为起始位置)来访问。
访问第二个 <p> 元素可以是以下代码:
HTMLCollection 对象 length 属性
HTMLCollection 对象的 length 属性定义了集合中元素的数量。
实例
实例解析
获取 <p> 元素的集合:
var myCollection = document.getElementsByTagName("p");
显示集合元素个数:
document.getElementById("demo").innerHTML = myCollection.length;
集合 length 属性常用于遍历集合中的元素。
实例
修改所有 <p> 元素的背景颜色:
注意
HTMLCollection 不是一个数组!
HTMLCollection 看起来可能是一个数组,但其实不是。
你可以像数组一样,使用索引来获取元素。
HTMLCollection 无法使用数组的方法: valueOf(), pop(), push(), 或 join() 。
点击查看所有 Javascript 教程 文章: https://www.codercto.com/courses/l/27.html
The Linux Programming Interface
Michael Kerrisk / No Starch Press / 2010-11-6 / GBP 79.99
The Linux Programming Interface describes the Linux API (application programming interface)-the system calls, library functions, and other low-level interfaces that are used, directly or indirectly, b......一起来看看 《The Linux Programming Interface》 这本书的介绍吧!