[疑问]作了一个用Js遍历树节点的函数,后来发现一个问题!
我的树是从数据库中读数据生成的
发现只有当节点都展开的情况下,才能遍历所有节点,否则报错,不知道是不是bug
function getV1(node)
{document.all("Hidden1").value="";
var NodeArray=new Array();
NodeArray=node.getChildren();
if (parseInt(NodeArray.length)!=0)
{
for(var i=0;i<NodeArray.length;i++)
{
var cNode=NodeArray[i];
//alert(cNode.getAttribute("Text"));
getV1(cNode);
}
}
else
{
if (node.getAttribute("checked")==true)
{
document.all("Hidden1").value=document.all("Hidden1").value+node.getAttribute("ID")+"#";
}
}
//alert(document.all("Hidden1").value);
}
发现只有当节点都展开的情况下,才能遍历所有节点,否则报错,不知道是不是bug
function getV1(node)
{document.all("Hidden1").value="";
var NodeArray=new Array();
NodeArray=node.getChildren();
if (parseInt(NodeArray.length)!=0)
{
for(var i=0;i<NodeArray.length;i++)
{
var cNode=NodeArray[i];
//alert(cNode.getAttribute("Text"));
getV1(cNode);
}
}
else
{
if (node.getAttribute("checked")==true)
{
document.all("Hidden1").value=document.all("Hidden1").value+node.getAttribute("ID")+"#";
}
}
//alert(document.all("Hidden1").value);
}
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<iewc:treeview id="TreeView1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server"></iewc:treeview><INPUT id="Hidden1" style="Z-INDEX: 102; LEFT: 296px; POSITION: absolute; TOP: 296px" type="hidden"
name="Hidden1" runat="server">
<asp:button id="Button1" style="Z-INDEX: 103; LEFT: 456px; POSITION: absolute; TOP: 464px" runat="server"
Text="Display"></asp:button></form>
<script>
getV1(TreeView1);
</script>
</body>
<form id="Form1" method="post" runat="server">
<iewc:treeview id="TreeView1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server"></iewc:treeview><INPUT id="Hidden1" style="Z-INDEX: 102; LEFT: 296px; POSITION: absolute; TOP: 296px" type="hidden"
name="Hidden1" runat="server">
<asp:button id="Button1" style="Z-INDEX: 103; LEFT: 456px; POSITION: absolute; TOP: 464px" runat="server"
Text="Display"></asp:button></form>
<script>
getV1(TreeView1);
</script>
</body>