前些时无聊,用javascript写的一颗树

类别:编程语言 点击:0 评论:0 推荐:

<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"> 
<script language="javascript">
function addLeaf(parentID)
{
    var leafHTML='';
    var bh=0;
    var node_path,parent;
    bh = parseInt(lsh.value)+1;1
    lsh.value = bh;
    if(parentID != 0)
    {
        parent = document.getElementById('tree_path_' + parentID);
        node_path = parent.value + ',' + bh;
        parent = document.getElementById('tree_UL_' + parentID);
    }
    else
    {
        parent = document.getElementById('tree_div_0');
        node_path = '0,' + bh;
    }
   // alert('');
    leafHTML = '<div id=tree_div_' + bh + '>';
    leafHTML = leafHTML + '<LI id=tree_LI_' + bh + ' >'; 
    leafHTML = leafHTML + '<input id=tree_nodeName_' + bh;
    leafHTML = leafHTML + ' onclick=newparent.value=' + bh + '; onkeydown=changesize(tree_nodeName_' + bh + '); type=text style=border:none;cursor:hand size=6 value=新节点>';
   
    leafHTML = leafHTML + '<input id=tree_path_' + bh + ' type=hidden value=' + node_path + '>';
    leafHTML = leafHTML + '<input id=tree_parent_' + bh + ' type=hidden value=' + parentID + '>';
    leafHTML = leafHTML + '<input id=tree_state_' + bh + ' type=hidden value=insert>';
    leafHTML = leafHTML + '<input id=tree_change_' + bh + ' type=hidden value=insert>';
    leafHTML = leafHTML + '<input id=tree_vision_' + bh + ' type=hidden value=1>';
   
    leafHTML = leafHTML + '<input type=button  value=加子节点 onclick=addLeaf(' + bh + ');>';
    leafHTML = leafHTML + '<input type=button  value=删该节点 onclick=delLeaf(' + bh +');>';
    leafHTML = leafHTML + '<input type=button  value=调整位置 onclick=changeParent(' + bh + ');>' + '</LI>' ;
    leafHTML = leafHTML + '<UL id=tree_UL_' + bh + '></UL>' ;
    leafHTML = leafHTML + '</div>';
    parent.innerHTML = parent.innerHTML + leafHTML;
}  

function delLeaf(nodeID)
{

    var temp_node;
    temp_node = document.getElementById('tree_div_' + nodeID);
    if(temp_node != null)
     {
         temp_node.style.display = 'none';
         temp_node = document.getElementById('tree_state_' + nodeID);
         temp_node.value = 'delete';
         temp_node = document.getElementById('tree_change_' + nodeID);
         temp_node.value = temp_node.value + ', delete';
     }
}  

function hidenode(nodeID)
{
   var leafUL;
   if(LeafID != null)
   {   
       leafUL = document.getElementById('node_' + nodeID);
       if(leafUL != null)
       {
      // alert(leafUL.id + '**' + LeafID.id);
           leafUL.style.display='none';
           LeafID.style.display='none'; 
       }
  }
}

function changesize(node)
{
  if(node != null)
   {   
       node.size = node.value.length * 2;
   } 
}

function changeParent(nodeID)
{   
    var node,parent,tempNode;
    var tempStr,tempNodeID;
    if(newparent.value == null || newparent.value == '')
    {
        alert('请选择一个节点作为父节点!!!');
        return false;
    }
    node = document.getElementById('tree_div_' + nodeID);
    parent = document.getElementById('tree_UL_' + newparent.value);
    if(node != null && parent !=null)
    {       
        tempNode = document.getElementById('tree_path_' + newparent.value);
        tempStr = tempNode.value;
        if(tempStr.lastIndexOf(',' + nodeID) == (tempStr.length-nodeID.toString().length-1))
        {
            alert('请不要选择自己作为父节点!!!');
            return false;
        }
        else
        {
            if(tempStr.indexOf(',' + nodeID + ',') != -1)
            {
                alert('请不要选择子节点作为父节点!!!');
                return false;
            }
            else
            {
                tempNode = document.getElementById('tree_path_' + nodeID);
                if((tempStr + ',' + nodeID ) == tempNode.value)
                {
                    alert('您选择的已经是此节点的父节点!!!');
                    return false;
                }
            }
        }
       
        tempNodeID = node.id;
        tempStr = node.innerHTML;
       // node.innerHTML = 'blank'; 
        //node.style.display = 'none';
       // node.id = 'del_' + tempNodeID;
        node.outerHTML ='';        
       
        parent.innerHTML = parent.innerHTML + '<div id=' + tempNodeID + '></div>';
        tempNode = document.getElementById(tempNodeID);
        tempNode.innerHTML = tempStr; 
               
        tempNode = document.getElementById('tree_path_' + nodeID);
        parent = document.getElementById('tree_path_' + newparent.value);
        tempNode.value = parent.value + ',' + nodeID;
       
        tempNode = document.getElementById('tree_parent_' + nodeID);
        tempNode.value = parent.value + ',' + newparent.value;
       
        tempNode = document.getElementById('tree_change_' + nodeID);       
        tempNode.value = tempNode.value + ',changeParent';
       
        tempNode = document.getElementById('tree_state_' + nodeID);       
        tempNode.value = 'changeParent';
       
        updateNode(nodeID);
       
    }
}

function updateNode(parentID)
{
    var tempNode,tempParent,nodeList;
    var i=0;
    tempParent = document.getElementById('tree_path_' + parentID);
    nodeList = document.getElementsByTagName('input');
    for(i=0;i<nodeList.length;i++)
    {
       if(nodeList(i).id.substring(0,12) == 'tree_parent_' && nodeList(i).value == parentID)
       {
           tempNode = document.getElementById('tree_path_' + nodeList(i).id.substring(12,nodeList(i).id.length));
           tempNode.value = tempParent.value + ',' + nodeList(i).id.substring(12,nodeList(i).id.length);
          
           updateNode(nodeList(i).id.substring(12,nodeList(i).id.length));
       }
    }
}
</script>
</head>
<body>
<input type=button id="ldsk" value="点击增加节点" onclick="addLeaf(0);"><input id="newparent" type=text  value=''>
<input type=button id="ldssk" value="显示选定节点的源码" onclick="getdiv.value = tree_div_0.innerHTML;"><br>
<ul id="tree_div_0" ></ul>  
<br><textarea id="getdiv" cols=80 rows=23></textarea>  
<input id="lsh" type=hidden  value='0'>
</body>
</html>

本文地址:http://com.8s8s.com/it/it23632.htm