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

(100分)JS根据父ID,获取所有子ID问题。
JScript code

var video =[
            { id:1, pId:0, name:"Video_Data 1", open:true},
            { id:11, pId:1, name:"Video_Data 1-1", open:true},
            { id:111, pId:11, name:"Video_Data 1-1-1"},
            { id:112, pId:11, name:"Video_Data 1-1-2"},
            { id:12, pId:1, name:"Video_Data 1-2", open:true},
            { id:121, pId:12, name:"Video_Data 1-2-1"},
            { id:122, pId:12, name:"Video_Data 1-2-2"},
            { id:2, pId:0, name:"Video_Data 2", checked:true, open:true},
            { id:21, pId:2, name:"Video_Data 2-1"},
            { id:22, pId:2, name:"Video_Data 2-2", open:true},
            { id:221, pId:22, name:"Video_Data 2-2-1", checked:true},
            { id:222, pId:22, name:"Video_Data 2-2-2"},
            { id:23, pId:2, name:"Video_Data 2-3"}
        ];



根据父ID获取子ID,几级节点不定。

------解决方案--------------------
JScript code

function getSubId(node) {
    $.each(video, function(index, currentNode) {
        if (currentNode.pId == node.id) {
            result.push(currentNode);
            getSubId(currentNode);
        }
    });
}

------解决方案--------------------
<script type="text/javascript">
var video =[
{ id:1, pId:0, name:"Video_Data 1", open:true},
{ id:11, pId:1, name:"Video_Data 1-1", open:true},
{ id:111, pId:11, name:"Video_Data 1-1-1"},
{ id:112, pId:11, name:"Video_Data 1-1-2"},
{ id:12, pId:1, name:"Video_Data 1-2", open:true},
{ id:121, pId:12, name:"Video_Data 1-2-1"},
{ id:122, pId:12, name:"Video_Data 1-2-2"},
{ id:2, pId:0, name:"Video_Data 2", checked:true, open:true},
{ id:21, pId:2, name:"Video_Data 2-1"},
{ id:22, pId:2, name:"Video_Data 2-2", open:true},
{ id:221, pId:22, name:"Video_Data 2-2-1", checked:true},
{ id:222, pId:22, name:"Video_Data 2-2-2"},
{ id:23, pId:2, name:"Video_Data 2-3"}
];
var test=[];
function getNode(id){
  for(var i=0;i<video.length;i++){
if(video[i]["pId"]==id){
alert(video[i]["id"]);
test.push(video[i]);
getNode(video[i]["id"]);
}
}
}
getNode(1);
for(var i=0;i<test.length;i++){
document.write(test[i]["id"]+"<br/>");
}
</script>
这样试试
------解决方案--------------------
需求有点不清楚
------解决方案--------------------
如果是直接的子节点,那上面已经有答案了,如果要获取所有的子节点,那你就递归吧。
------解决方案--------------------
探讨

如果是直接的子节点,那上面已经有答案了,如果要获取所有的子节点,那你就递归吧。

------解决方案--------------------
我怎么就没看懂楼主的问题呢