(二)类数组对象HTMLCollection

HTMLCollection 表示 HTML 元素的集合。

下面的几种方式将返回 HTMLCollection对象:

html:

<body>
    <ul id="box">
      <li>节点一</li>
      <li>节点二</li>
      <li>节点三</li>
    </ul>

    <table border="1">
      <tr id="tr">
        <td>第一行</td>
        <td name="td">第二行</td>
        <td name="td">第三行</td>
      </tr>
    </table>

    <img src="duer.jpg" alt="img1" />
    <img src="ipone6s+.png" alt="img2" />

    <form action="">
      <input type="text" value="用户名">
    </form>
    <form action="">
      <input type="text" value="密码">
    </form>

    <a href="#">忘记密码</a>
    <a href="#">更多内容</a>

    <select id="select">
      <option value="0">北京</option>
      <option value="1">天津</option>
      <option value="2">河北</option>
    </select>

    <p>DOM探索之基础详解篇</p>
    <p>DOM探索之节点操作篇</p>
  </body>

JS:

        var scripts = document.scripts;
        var links = document.links;
        var cells = document.getElementById("tr").cells;
        var imgs = document.images;
        var forms = document.forms;
        var options = document.getElementById("select").options;
        var ps = document.getElementsByTagName("p");

        console.log(scripts);
        console.log(links);
        console.log(cells);
        console.log(imgs);
        console.log(forms);
        console.log(options);
        console.log(ps);

结果如下:

 

posted @ 2018-02-08 13:40  安静的女汉纸  阅读(312)  评论(0编辑  收藏  举报