document.all理解

   The all collection includes one element object for each valid HTML tag.

If a valid tag has a matching end tag, both tags are represented by the same element object.

all集合包含每个有效标签构成的元素对象。

是由微软增加的,IE11不再支持all集合,其他浏览器也支持此对象,能不用尽量不要用。

例子:

<!DOCTYPE html>
<html>
  <head>
    <title>validation</title>
    <meta charset="utf-8" />
    <script type="text/javascript">
        window.onload = function() {
            var html = "<table border='1' style='font-size:12px;'>";
            function getElement(sec) {
                if (sec)
                {
                     html += "<tr><td>" + sec + "</td>" + "<td>" + eval(sec).tagName + "</td>";
                } 
            }
            getElement("document.all(0)");
            getElement("document.all[0]");
            getElement("document.all.item(0)");
            getElement("document.all('SPAN1')");
            getElement("document.all.SPAN1");
            getElement("document.all['SPAN1']");
            getElement("document.all.namedItem('SPAN1')");
            html += "</table>";
            var names = "<br>";
            for (var i = 0; i < document.all.length ; i++)
            {
                 names += document.all[i].tagName + ": " + document.all[i].id + "; ";
            }
            document.getElementById("names").innerHTML = html + names;
        }
    </script>
  </head>

  <body id="aaa">
    <span id="SPAN1"></span>

    <div id="info">
        <div id="names">
        </div>
    </div>
  </body>
</html>

上面的结果是:

原文地址:https://www.cnblogs.com/ahudyan-forever/p/5702792.html