52. Element item() 方法

来源:互联网 发布:视频剪辑软件中文 编辑:程序博客网 时间:2024/06/03 14:36

实例

返回元素的首个子节点:

document.body.childNodes.item(0);

<body><p id="demo">点击按钮来获得 body 元素首个子节点的名称。</p><button onclick="myFunction()">试一下</button><script>function myFunction(){var x=document.getElementById("demo");  x.innerHTML=document.body.childNodes.item(0).nodeName;}</script></body>

定义和用法

item() 方法节点列表中位于指定索引的节点。

以下两条语法产生相同的结果:

document.body.childNodes.item(0);document.body.childNodes[0]
0 0