在IE里,你设置了<a name="abc" 这个时候IE认为这个控件的id也被等同地赋值为abc,所以用 getElementById 会把第一个只赋 name 的控件找出来,还有比如其它的表单元素和框架元素input select frame iframe 等在IE里都会有这种结果

解决方案 »

  1.   

    我是这样理解的,不知道对不对
    ID只返回一个对象,就是第一个ID或者NAME为参数的那个对象,NAME返回多个NAME或者ID为参数的那个对象
      

  2.   

    to  meizz(梅花雪)其实有name这个属性的tag不是很多,我不知道是不是你说的那个原因。如果对于第一个可以说的通的话,第二个怎么解释,
    为什么用name取,也能把tag为div的取出来,而实际上这个tag不具有name属性,而且我也没有设
      

  3.   

    晕,我被搞晕了,IE里怎么对id和name这么混乱地乱用呀!
      

  4.   

    我自己写代码的时候,基本上整个页面里的 id 和 name 都不会重复的(checkbox 组不算),所以一直都没有发现过这样的问题。
      

  5.   

    id应该是唯一标识,name允许重复,这或许是设计者的本意====CSDN====
    人生起起伏伏凑字凑
      

  6.   

    晕,本来上面两个想明白了,再加了一个我又晕了
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    </HEAD>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    function showTagById(){
    var a = document.getElementById("abc");
    if(a==null){
    alert("can't find element.");
    }else{
    alert(a.outerHTML);
    }}
    function showTagByName(){
    var a = document.getElementsByName("abc");
    if(a[0]==null){
    alert("can't find element.");
    }else{
    for(var i=0; i<a.length; i++){
    alert(a[i].outerHTML);
    }
    }
    }
    //-->
    </SCRIPT>
    <BODY>
    <a name = "abc">It's Weird, a-name-abc</a>
    <div id = "abc">It's Weird, div-id-abc</div>
    <input id = "abc" type=text value="It's Weird, div-id-abc">
    <INPUT TYPE="button" value="showTagById" onclick="showTagById();">
    <INPUT TYPE="button" value="showTagByName" onclick="showTagByName();">
    </BODY>
    </HTML>
      

  7.   

    to  lunge() 我的问题和你的回答没有关系,我只是说为什么用name取,却把没有设name的element取出来了。
      

  8.   

    http://www.w3.org/TR/1998/REC-html40-19980424/struct/links.html#anchors-with-id
    The following example names a destination anchor with the id attribute: I just returned from vacation! Here's a
    <A id="anchor-two">photo of my family at the lake.</A>.
    The id and name attributes share the same name space. This means that they cannot both define an anchor with the same name in the same document. 
      

  9.   

    to fantiny(虽然我是菜鸟,不过我是~~非暴力不合作~~的菜鸟)也就是说firefox没有严格按照w3标准来设计了???那么关于div的问题怎么解释???
      

  10.   

    When you use the IHTMLDocument3::getElementsByName method, all elements in the document that have the specified NAME or ID attribute value are returned.
      

  11.   

    to  fantiny(虽然我是菜鸟,不过我是~~非暴力不合作~~的菜鸟) 这样设计有什么好处???
      

  12.   

    我发现IE和opera对这两个方法的应用很混乱,但是firefox就很清晰,
    难道真是w3的标准??
    它到底要干什么?
      

  13.   

    w3c为什么要name和id共用 name space,你可以自己去查下。
    ie接口这么设计,我觉得是microsoft偷懒吧。