<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>
<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>
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.
id 是唯一性的标志