document.all 为空异常

document.all 为空错误代码:htmlheadscripttype text/javascript functionadd(){varmyTabledocu

document.all 为空错误
代码:

<html>
<head>
<script   type= "text/javascript ">
function   add(){
var   myTable   =   document.getElementById( 'myTb ');   //获取table中tbody的id的对象
var   i=1;
var   newTr   =   document.createElement( "tr ");//创建table中tbody的tr对象

var   newTd1   =   document.createElement( "td ");
var   newTd2   =   document.createElement( "td ");
var   newTd3   =   document.createElement( "td ");
var   newTd4   =   document.createElement( "td ");

var   newCheckbox   =   document.createElement( "input ");
var   newText1   =   document.createElement( "input ");
var   newText2   =   document.createElement( "input ");
var   newText3   =   document.createElement( "input ");

newCheckbox.type= "checkbox ";
newCheckbox.id= "mycheck ";
newCheckbox.defaultChecked=false;

newText1.type= "text ";
newText2.type= "text ";
newText3.type= "text ";


newTd1.appendChild(newCheckbox);
newTd2.appendChild(newText1);
newTd3.appendChild(newText2);
newTd4.appendChild(newText3);

newTr.appendChild(newTd1);
newTr.appendChild(newTd2);
newTr.appendChild(newTd3);
newTr.appendChild(newTd4);

myTable.appendChild(newTr);
i++;
}

function   del(){
var   row   =   document.all.mycheck.length;
//var   myTable   =   document.getElementById( 'myTb ');
var   count=0;
for(i=0;i <row;i++){
var   c   =   document.all.mycheck[i].type;
alert(c== "checkbox ");
while(document.all.mycheck[i].checked){
//var   childNum   =   document.all.myTb.children.length;
//alert(childNum);
document.all.myTb.deleteRow(i+1);
//var   delTr   =   document.all.myTb.children(i+1);
//myTable.removeChild(delTr);
count++;
}
}
if(count==0){
alert( "   select   ");
}
}
</script>
</head>
<body>
<form>
<input   type= "button "   onclick= "add() "   value= "     add     "> &nbsp;&nbsp;
<input   type= "button "   onclick= "del() "   value= "     del     ">
</form>

<table   border= "1 ">
<tbody   id= "myTb ">
<tr>
<th>     FLOG   </th>
<th>   &nbsp;&nbsp;   A   &nbsp;&nbsp; </th>
<th>   &nbsp;&nbsp;   B   &nbsp;&nbsp; </th>
<th>   &nbsp;&nbsp;   C   &nbsp;&nbsp; </th>
</tr>
</tbody>
</table>

<form>
<input   type= "button "   onclick= "add() "   value= "     add     ">     &nbsp;&nbsp;
<input   type= "button "   onclick= "del() "   value= "     del     ">
</form>
</body>
</html>


错误:

'document.all.mycheck[...].checked '   为空或不是对象





[解决办法]
你每删掉一行,table的rows.length就减1,也就是说是动态变化的,所以你那个就超长了。一般都是从最大的开始遍历,就可以不用考虑这个问题了。
<html>
<head>
<script type= "text/javascript ">
function add(){
var myTable = document.getElementById( 'myTb '); //获取table中tbody的id的对象
var i=1;
var newTr = document.createElement( "tr ");//创建table中tbody的tr对象

var newTd1 = document.createElement( "td ");
var newTd2 = document.createElement( "td ");
var newTd3 = document.createElement( "td ");
var newTd4 = document.createElement( "td ");

var newCheckbox = document.createElement( "input ");
var newText1 = document.createElement( "input ");
var newText2 = document.createElement( "input ");
var newText3 = document.createElement( "input ");

newCheckbox.type= "checkbox ";
newCheckbox.id= "mycheck ";
newCheckbox.defaultChecked=false;

newText1.type= "text ";
newText2.type= "text ";
newText3.type= "text ";


newTd1.appendChild(newCheckbox);
newTd2.appendChild(newText1);
newTd3.appendChild(newText2);
newTd4.appendChild(newText3);

newTr.appendChild(newTd1);
newTr.appendChild(newTd2);
newTr.appendChild(newTd3);
newTr.appendChild(newTd4);

myTable.appendChild(newTr);
i++;
}

function del(){
if (document.all.mycheck == null)
{
alert( "null ");
return false;
}
var row = document.all.mycheck.length;
//var myTable = document.getElementById( 'myTb ');
var count=0;
for(i=row-1;i> =0;i--){
var c = document.all.mycheck[i].type;
//alert(c== "checkbox ");
if (document.all.mycheck[i].checked){
//var childNum = document.all.myTb.children.length;
//alert(childNum);
document.all.myTb.deleteRow(i+1);
//var delTr = document.all.myTb.children(i-1);
//myTable.removeChild(delTr);
count++;
}
}
if(count==0){
alert( " select ");
}
}
</script>
</head>
<body>
<form>
<input type= "button " onclick= "add() " value= " add "> &nbsp;&nbsp;
<input type= "button " onclick= "del() " value= " del ">
</form>

<table border= "1 ">
<tbody id= "myTb ">
<tr>
<th> FLOG </th>
<th> &nbsp;&nbsp; A &nbsp;&nbsp; </th>
<th> &nbsp;&nbsp; B &nbsp;&nbsp; </th>
<th> &nbsp;&nbsp; C &nbsp;&nbsp; </th>
</tr>
</tbody>
</table>

<form>
<input type= "button " onclick= "add() " value= " add "> &nbsp;&nbsp;
<input type= "button " onclick= "del() " value= " del ">
</form>
</body>
</html>