Blogger Information
Blog 54
fans 4
comment 1
visits 54816
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Document.getElementsByTagName()
神仙不在的博客
Original
846 people have browsed it

1、返回一个包括所有给定标签名称的元素的HTML集合HTMLCollection,是个伪数组

2、可以用for循环遍历打印所有元素

3、就算是只有一个元素,那也是集合。

4、如果页面没有这个元素,返回的是空伪数组

5、父元素是一个单个对象(指明的元素)list.getElementsByTagName('li');

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<ul>
    <li>我是第一个</li>
    <li>我是第二个</li>
    <li>我是第三个</li>
    <li>我是第四个</li>
    <li>我是第五个</li>
</ul>
<ol id="list">
    <li>name1</li>
    <li>name2</li>
    <li>name3</li>
    <li>name4</li>
    <li>name5</li>
</ol>
<script>
    var list = document.getElementById('list');
    var lis = list.getElementsByTagName('li');
    console.log(typeof lis);
    console.log(lis);
</script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post