这样你就可以得到5了
<input type=button value=getTagName onclick="getTagName()">
<div id="up">123</div>
<div id="up">456</div>
<div id="up">789</div>
<input name="up">
<input name="up">
<script language="javascript">
   function getTagName()
   {
        var names=document.getElementsByName("up");
        alert(names.length)
   }
</script>

解决方案 »

  1.   

    看清楚了,是ByName,你的元素是不一样了,可是名字一样怎么取呀,没什么特殊要求的话,不要取相同的名字,一面造成不必要的麻烦。
      

  2.   

    看清楚了,是ByName,你的元素是不一样了,可是名字一样怎么取呀,没什么特殊要求的话,不要取相同的名字,一面造成不必要的麻烦。
      

  3.   

    页面元素<div>里要用id而不是用name为其标记,
    页面元素<input>用name就可以为其标记了,这两种元素在页面中来说,类型是不一样的,所以它的唯一定位也不一样。
      

  4.   

    为什么这样?那上面的DIV用getElementById()还能得到吗?
    undefined???!!!费解!
      

  5.   

    getElementById()用于没有重复的场合。
    getElementsByTagName()用于任何场合
      

  6.   

    getElementById()用于没有重复的场合。
    getElementsByTagName()用于任何场合
      

  7.   

    将所有的name 属性改成 id 属性,也能得到 5
      

  8.   

    因为div没有名字属性。你写的name=up属于expando 属性,不能包括进去,所以是2啦。When you use the getElementsByName method, all elements in the document that have the specified NAME or ID attribute value are returned.Elements that support both the NAME and the ID attribute are included in the collection returned by the getElementsByName method, but not elements with a NAME expando.
             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~