var oObject = document.all.item("sample");
if (oObject != null){
   if (oObject.length != null){
      for (i = 0; i < oObject.length; i++){
         alert(oObject(i).tagName);
      }
   }
}
else{
   alert(oObject.tagName);
}

解决方案 »

  1.   

    错了,是这个
    for(i = 0; i < document.all.length; i++){
       alert(document.all(i).tagName);
    }
      

  2.   

    我是想要元素的名字,tagName没有的:(
      

  3.   

    遍历文档
    <head>
    <script>
    function countTags(n){
    var numtags=0;
    if(n.nodeType==1)numtags++;
    var children=n.childNodes;
    for(var i=0;i<children.length;i++){
    numtags+=countTags(children[i])};
    return numtags;
    }
    </script>
    </head>
    <body onload="alert('这个文档含有'+countTags(document)+'个标记')">
    this is a<i> sample</i>
    <body>
      

  4.   

    <script>
    window.onload = takeTag;
    function takeTag()
    {
    for(i = 0; i < document.all.length; i++){
    alert(document.all(i).name);
    }
    }
    </script>