日期:2014-05-16  浏览次数:20301 次

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>