Copy Code code as follows:
<script language= "JavaScript" >
/*
Function name: Checknode (CurrentNode), ParentNode (CurrentNode), Childnode (CurrentNode)
function function: To implement a TreeView with a checkbox
1, select the parent node of its child nodes are also selected.
2, the elimination of all the sub-node selection, the choice of the parent node will also be canceled
How to use:
1. Add Checknode (CurrentNode), ParentNode (CurrentNode), Childnode (CurrentNode) in the middle of
2. Add YourTreeView.Attribute.Add to the Page_Load () event in *.aspx.cs ("Oncheck", "Checknode ( Yourtreeview.clickednodeindex)) ")
*/
Recursively traverse all child nodes
function Checknode (CurrentNode)
{
var childnode=new Array ();
var parentnodechild=new Array ();
var ischecked;
Childnode=currentnode.getchildren ();
if (Currentnode.getattribute (' checked '))
{
Ischecked=true;
}
Else
{
Ischecked=false;
}
Parent node Processing
if (Currentnode.getparent ()!=null)
{
Check processing
if (Currentnode.getattribute (' Checked '))
{
ParentNode (CurrentNode);
}
Else
Uncheck
{
Childnode (CurrentNode);
}
}
Else
{
Don't do anything.
}
Child node Processing
if (childnode.length>0)
{
for (Var i=0;i<childnode.length;i++)
{
Childnode.setattribute ("Checked", ischecked);
if (Childnode.getchildren (). length>0)
{
Checknode (Childnode);
}
}
}
}
Recursively Select parent node
function ParentNode (CurrentNode)
{
if (Currentnode.getparent ()!=null)
{
Currentnode.getparent (). setattribute (' Checked ', true);
Recursively calls ParentNode (CurrentNode) to traverse a parent node on a higher level
ParentNode (Currentnode.getparent ());
}
}
Recursively uncheck parent node
function Childnode (CurrentNode)
{
if (Currentnode.getparent ()!=null)
{
var checkedcount=0;
var childnode=currentnode.getparent (). GetChildren ();
for (Var i=0;i<childnode.length;i++)
{
if (Childnode.getattribute (' Checked '))
{
checkedcount++;
}
}
if (checkedcount==0)
{
Currentnode.getparent (). setattribute (' Checked ', false);
}
Recursively calls Childnode (CurrentNode) to traverse a parent node on a higher level
Childnode (Currentnode.getparent ());
}
}
</script>
The problem of JS recursively traversing all child nodes of a node in the TreeView
Copy Code code as follows:
var allrootnode=new Array ();
Allrootnode=treeview1.getchildren ();
Alertnode (Allrootnode);
function Alertnode (Nodearray)
{
if (parseint (nodearray.length) ==0)
Return
Else
{
for (i=0;i<nodearray.length;i++)
{
var Cnode;
Cnode=nodearray;
Alert (Cnode.getattribute ("Text"));
if (parseint (Cnode.getchildren (). Length)!=0)
Alertnode (Cnode.getchildren ());
}
}
}