<body>
<div id="test"></div>
</body>
<script language="JavaScript">
<!--
var test = document.getElementById("test");
var input = document.createElement("input");
input.setAttribute("type","checkbox");
input.setAttribute("id","test1");
input.setAttribute("name","test1");
test.appendChild(input); var test1 = document.getElementsByName("test1");
alert(test1.length);
//-->
</script>

解决方案 »

  1.   

    var input = document.createElement("<input name='test1'></input>");
    Name属性必须特殊对待,只要如此创建就能找到了。MSDN原文如此:
    Attributes can be included with the sTag as long as the entire string is valid HTML. You should do this if you wish to include the NAME attribute at run time on objects created with the createElement method.
      

  2.   

    zhaoxiaoyang(梅雪香@深圳) 的方法其实是利用了getElementsByName方法会搜索id来达到目的,但这跟我上面的方法的原理是不同的。
      

  3.   

    如果用 id 的方法肯定会有问题 firefox 下不能通过的 
    id 是唯一性的标志