var chk = document.createElement( "input");
chk.setAttribute( "name", "day");
chk.setAttribute( "type", "checkbox");
chk.setAttribute( "ID", idstr);
chk.setAttribute( "value", arrvalue);
chk.setAttribute( "class", "datechk");
var oTextNode = document.createTextNode(arrvalue);
var spn = document.createElement("span");
spn.setAttribute( "ID", "span"+idstr);
spn.appendChild(chk);
spn.appendChild(oTextNode);
liobj.appendChild(spn);
查了一下发现 "input在IE中生成时有name属性,怎么办呢
chk.setAttribute( "name", "day");
chk.setAttribute( "type", "checkbox");
chk.setAttribute( "ID", idstr);
chk.setAttribute( "value", arrvalue);
chk.setAttribute( "class", "datechk");
var oTextNode = document.createTextNode(arrvalue);
var spn = document.createElement("span");
spn.setAttribute( "ID", "span"+idstr);
spn.appendChild(chk);
spn.appendChild(oTextNode);
liobj.appendChild(spn);
查了一下发现 "input在IE中生成时有name属性,怎么办呢
<script>
window.onload=function(){
var obj=document.getElementById('SelectType');
for(i=0;i<2;i++)
{
var liobj = document.createElement( "li");
for(ii=0;ii<5;ii++)
{
var chk = document.createElement( "input");
chk.setAttribute( "name", "Cc[]");
chk.setAttribute( "type", "checkbox");
chk.setAttribute( "ID","ClassID");
chk.setAttribute( "value", "100");
chk.setAttribute( "class", "datechk");
var oTextNode = document.createTextNode("第项");
var spn = document.createElement("span");
spn.appendChild(chk);
spn.appendChild(oTextNode);
liobj.appendChild(spn); }
obj.appendChild(liobj);
}
}
</script>
<body>
<form name="mainform" action="" method="post">
<div id="SelectType"></div>
</form>
</body>
<script>
alert(document.getElementById('SelectType').innerHTML);
var len = document.mainform.day.length;
</script>
window.onload=function(){
var obj=document.getElementById('SelectType');
for(i=0;i<2;i++)
{
var liobj = document.createElement( "li");
for(ii=0;ii<5;ii++)
{
var chk = document.createElement( "input");
chk.setAttribute( "name", "Cc[]");
chk.setAttribute( "type", "checkbox");
chk.setAttribute( "ID","ClassID");
chk.setAttribute( "value", "100");
chk.setAttribute( "class", "datechk");
var oTextNode = document.createTextNode("第项");
var spn = document.createElement("span");
spn.appendChild(chk);
spn.appendChild(oTextNode);
liobj.appendChild(spn); }
obj.appendChild(liobj);
}
}
</script>
<body>
<form name="mainform" action="" method="post">
<div id="SelectType"></div>
</form>
</body>
<script>
alert(document.getElementById('SelectType').innerHTML);
var len = document.all.mainform.day.length;
</script>
为了兼容使用id获取form
<script>
window.onload=function(){
var obj=document.getElementById('SelectType');
for(i=0;i<2;i++)
{
var liobj = document.createElement( "li");
for(ii=0;ii<5;ii++)
{
var chk = document.createElement( "input");
chk.setAttribute( "name", "Cc[]");
chk.setAttribute( "type", "checkbox");
chk.setAttribute( "ID","ClassID");
chk.setAttribute( "value", "100");
chk.setAttribute( "class", "datechk");
var oTextNode = document.createTextNode("第项");
var spn = document.createElement("span");
spn.appendChild(chk);
spn.appendChild(oTextNode);
liobj.appendChild(spn); }
obj.appendChild(liobj);
}
alert(document.getElementById('SelectType').innerHTML); }
</script>
<body>
<form id="myform" name="mainform" action="" method="post">
<div id="SelectType"></div>
</form>
</body>
<script>
</script>
重新做的示例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <head>
<title> new document </title> <meta name="generator" content="editplus" />
<meta name="author" content="Gao YiXiang" /> <meta name="email" content="[email protected]" /> <meta name="keywords" content="javascript dhtml dom" />
<meta name="description" content="I love web development." />
</head>
<body>
<form id="myform" name="mainform" action="" method="post">
<input name="parentId" type="checkbox" id="i1" />
<input name="parentId" type="checkbox" id="i2" />
<input name="parentId" type="checkbox" id="i3" />
<input name="parentId" type="checkbox" id="i4" />
<div id="SelectType"> </div>
</form>
<script type="text/javascript">
<!--
var obj=document.getElementById('SelectType');
for(i=0;i <2;i++)
{
var liobj = document.createElement( "li");
for(ii=0;ii <5;ii++)
{
var chk = document.createElement( "<INPUT></INPUT>");
chk.setAttribute( "type", "checkbox");
chk.setAttribute( "ID","ClassID");
chk.setAttribute( "value", "100");
chk.setAttribute( "class", "datechk");
chk.setAttribute( "Name", "day");
var oTextNode = document.createTextNode("abc");
var spn = document.createElement("span");
spn.setAttribute( "Name", "sday");
spn.appendChild(chk);
spn.appendChild(oTextNode);
liobj.appendChild(spn); }
obj.appendChild(liobj);
}
alert(document.getElementById('SelectType').innerHTML); var aT = document.getElementsByName('day');
// var aT = document.getElementsByName('parentId'); //ie下可得到值 ff没试
alert(aT[aT.length-1].id);
</script>
</body>
</html> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> <head>
<title> new document </title> <meta name="generator" content="editplus" />
<meta name="author" content="Gao YiXiang" /> <meta name="email" content="[email protected]" /> <meta name="keywords" content="javascript dhtml dom" />
<meta name="description" content="I love web development." />
</head>
<body>
<form id="myform" name="mainform" action="" method="post">
<input name="parentId" type="checkbox" id="i1" />
<input name="parentId" type="checkbox" id="i2" />
<input name="parentId" type="checkbox" id="i3" />
<input name="parentId" type="checkbox" id="i4" />
<div id="SelectType"> </div>
</form>
<script type="text/javascript">
<!--
var obj=document.getElementById('SelectType');
for(i=0;i <2;i++)
{
var liobj = document.createElement( "li");
for(ii=0;ii <5;ii++)
{
var chk = document.createElement( "<INPUT></INPUT>");
chk.setAttribute( "type", "checkbox");
chk.setAttribute( "ID","ClassID");
chk.setAttribute( "value", "100");
chk.setAttribute( "class", "datechk");
chk.setAttribute( "Name", "day");
var oTextNode = document.createTextNode("abc");
var spn = document.createElement("span");
spn.setAttribute( "Name", "sday");
spn.appendChild(chk);
spn.appendChild(oTextNode);
liobj.appendChild(spn); }
obj.appendChild(liobj);
}
alert(document.getElementById('SelectType').innerHTML); var aT = document.getElementsByName('day');
// var aT = document.getElementsByName('parentId'); //ie下可得到值 ff没试
alert(aT[aT.length-1].id);
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml"> <head>
<title> new document </title> <meta name="generator" content="editplus" />
<meta name="author" content="Gao YiXiang" /> <meta name="email" content="[email protected]" /> <meta name="keywords" content="javascript dhtml dom" />
<meta name="description" content="I love web development." />
</head>
<body>
<form id="myform" name="mainform" action="" method="post">
<input name="parentId" type="checkbox" id="i1" />
<input name="parentId" type="checkbox" id="i2" />
<input name="parentId" type="checkbox" id="i3" />
<input name="parentId" type="checkbox" id="i4" />
<div id="SelectType"> </div>
</form>
<script type="text/javascript">
<!--
var obj = document.getElementById('SelectType');
for(i=0;i <2;i++)
{
var liobj = document.createElement("li");
for(ii=0;ii <5;ii++)
{
var chk = document.createElement("<INPUT name='day'></INPUT>");
chk.setAttribute("type", "checkbox");
chk.setAttribute("id","ClassID");
chk.setAttribute("value", "100");
chk.setAttribute("class", "datechk");
var oTextNode = document.createTextNode("abc");
var spn = document.createElement("span");
spn.setAttribute("name", "sday");
spn.appendChild(chk);
spn.appendChild(oTextNode);
liobj.appendChild(spn);
}
obj.appendChild(liobj);
}
alert(document.getElementById('SelectType').innerHTML);
var aT = document.getElementsByName('day');
// var aT = document.getElementsByName('parentId'); //ie下可得到值 ff没试
alert(aT[aT.length-1].id);
</script>
</body>
</html>
ele_name = document.all? "<INPUT name='day'></INPUT>" : "input";
var chk = document.createElement(ele_name);
window.onload=function(){
var obj=document.getElementById('SelectType');
for(i=0;i<2;i++)
{
var liobj = document.createElement( "li");
for(ii=0;ii<5;ii++)
{
var chk = document.createElement( "input");
chk.setAttribute("Name", "day");
chk.setAttribute("type", "checkbox");
chk.setAttribute("id","ClassID");
chk.setAttribute("value", "100");
chk.setAttribute("class", "datechk");
var oTextNode = document.createTextNode("第项");
var spn = document.createElement("span");
spn.appendChild(chk);
spn.appendChild(oTextNode);
liobj.appendChild(spn); }
obj.appendChild(liobj);
}
document.getElementById('SelectType').innerHTML=document.getElementById('SelectType').innerHTML.replace(/Name/g,"name")
var aT = document.getElementsByName('day')[0];
alert(aT.id); }
</script>
<body>
<form name="mainform" action="" method="post">
<div id="SelectType"></div>
</form>
</body>
要注意大小写,Name开始大写,然后换为小写,这样在ie和ff下都通过了。
var chk = document.createElement( "<INPUT name='day'></INPUT>")获取:
var d = document.getElementsByName("day")
不用判断d.length,直接写即可
for(i = 0;i<d.length;i++)
ar chk = document.createElement("<INPUT ID='abc' type='checkbox' name='day' ></INPUT>") 报错
String contains an invalid character" code: "5
function ex()
{
for(i=0;i<10;i++)
{
var chk
if(navigator.appName == "Microsoft Internet Explorer")
{
chk = document.createElement("<INPUT name=day type='checkbox' />");
}
else
{
chk = document.createElement("INPUT")
chk.setAttribute("type","checkbox")
chk.setAttribute("name","day")
}
chk.setAttribute("value",i)
document.getElementById("mxh").appendChild(chk);
}
}function gex()
{var chk = document.getElementsByName("day");
for(i = 0;i<chk.length;i++)
alert(chk[i].checked + " = " + chk[i].value)}
</script>
<body>
<form name=f>
<input type=button value="生成" onclick="ex()" />
<input type=button value="获取" onclick="gex()" />
<div id="mxh"></div>
</form>
</body>
document.getElementById("mxh").innerHTML = "<input name='day' type='checkbox' /><input name='day' type='checkbox' /><input name='day' type='checkbox' />"
var day = document.getElementsByName("day");
var len = day.length;
window.onload=function(){
var obj=document.getElementById('SelectType');
for(i=0;i<2;i++)
{
var liobj = document.createElement( "li");
for(ii=0;ii<5;ii++)
{
var chk = document.createElement( "input");
chk.setAttribute("Name", "day");
chk.setAttribute("type", "checkbox");
chk.setAttribute("id","ClassID");
chk.setAttribute("value", "100");
chk.setAttribute("class", "datechk");
var oTextNode = document.createTextNode("第项");
var spn = document.createElement("span");
spn.appendChild(chk);
spn.appendChild(oTextNode);
liobj.appendChild(spn); }
obj.appendChild(liobj);
}
document.getElementById('SelectType').innerHTML=document.getElementById('SelectType').innerHTML.replace(/Name/g,"name")
var aT = document.getElementsByName('day')[0];
alert(aT.id); }
</script>
<body>
<form name="mainform" action="" method="post">
<div id="SelectType"></div>
</form>
</body>
要注意大小写,Name开始大写,然后换为小写,这样在ie和ff下都通过了。