论坛首页 入门技术论坛

为什么不是想要的结果

浏览 1171 次
该帖已经被评为新手帖
作者 正文
   发表时间:2008-06-10  
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script>
var treesData;
this.treesData=new Array();	
//数据结构:编号,父节点编号,是否存在叶子,显示文本,超级链接
this.treesData.push(['1','1',true,'JavaScript参考书','#']);
this.treesData.push(['11','1',false,'前言','#']);
this.treesData.push(['2','2',false,'联系我们','#']);
this.treesData.push(['12','1',false,'绪论','#']);
this.treesData.push(['142','14',false,'第二节','#']);
this.treesData.push(['141','14',false,'第一节','#']);	
this.treesData.push(['133','13',false,'第三节','#']);
this.treesData.push(['14','1',true,'第二章','#']);
this.treesData.push(['143','14',false,'第三节','#']);	
this.treesData.push(['13','1',true,'第一章','#']);
this.treesData.push(['132','13',false,'第二节','#']);
this.treesData.push(['131','13',false,'第一节','#']);	

//删除数组元素,index为下标
Array.prototype.removeBySplice = function(index)
{
	if(isNaN(index)||index>this.length){
		return false;
	}
	this.splice(index,1);
}
var theNode;
var flag;
function del(id)
{	
	alert(id);	
	for(var i=0;i<this.treesData.length;i++){
		if(this.treesData[i][0]==id)  //删除自己
		{	
			theNode=this.treesData[i][0];
			flag=this.treesData[i][2];
			this.treesData.removeBySplice(i);
				if(flag){									
					del(theNode);
				}		
				
		}else{
			if(this.treesData[i][1]==id)
			{
				theNode=this.treesData[i][0];
				flag=this.treesData[i][2];				
				this.treesData.removeBySplice(i);				
				if(flag)
				{					
					del(theNode);
				}				
			}		
		}
	}
	
}
function view(){
alert("数组大小:"+this.treesData.length);
for(var i=0;i<this.treesData.length;i++)
{
	alert(this.treesData[i][0]+":"+this.treesData[i][3]);
}

}
</script>
</head>

<body>
<input type="button" value="删除数组" onclick="del(1)"/>
<input type="button" value="数组大小" onclick="view(1)"/>
</body>
</html>

结果为什么会是3,怎么会这样?我想要的是1个元素啊,怎么解决?
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics