概述
ParentNode.children 是一个只读属性,返回一个包含当前元素的子元素的集合,该集合为一个即时更新的(live)HTMLCollection。
语法
var elList = elementNodeReference.children;
备注
children 属性为只读属性,对象类型为 HTMLCollection,你可以使用 elementNodeReference.children[1].nodeName 来获取某个子元素的标签名称。
例子
// parg是一个指向<p>元素的对象引用
if (parg.childElementCount)
// 检查这个<p>元素是否有子元素
// 译者注:childElementCount有兼容性问题
{
var children = parg.children;
for (var i = 0; i < children.length; i++)
{
// 通过children[i]来获取每个子元素
// 注意:List是一个live的HTMLCollection对象,在这里添加或删除parg的子元素节点,都会立即改变List的值.
};
};
规范
| Specification | Status | Comment |
|---|---|---|
| DOM ParentNode.children |
Living Standard | Initial definition. |
浏览器兼容性
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | 1 | 3.5 | 9 (IE6-8 incl commend nodes) | 10 | 4 |
| Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
[1] Internet Explorer 6 - 8 支持该属性,但是可能会错误地包含注释 Comment 节点。
相关链接
- The
ParentNodeandChildNodepure interfaces.