我有一个类似于数据树的json对象。我想交互式地在UI上显示它。在点击一个组件时,它的孩子应该被显示等等。我写了下面的代码for循环中动态创建的html元素的Onclick事件
var json_data = {
"component": "A",
"status": 0,
"children": [
{
"component": "AA",
"status": 0,
"children": [
{
"component": "AAA",
"status": 0,
"children": []
},
{
"component": "AAB",
"status": 2,
"children": []
}
]
},
{
"component": "AB",
"status": 0,
"children": [
{
"component": "ABA",
"status": 0,
"children": []
},
{
"component": "ABB",
"status": 1,
"children": []
}
]
}
]
};
$(document).ready($(function(){
var $result = $('#result');
start(json_data)
//Root Node display
function start(obj)
{
$('
').text(obj.component + '-'+obj.status).appendTo($result);$("#"+obj.component).click(function(){
displaychildren(obj);
});
}
Display the children of an element on click by recursion
function displaychildren(node)
{
$.each(node.children,function (i,v){
$('
').text(v.component + '-'+v.status).appendTo($result);if$("#"+v.component).click(function(){
alert("Problem is here");
displaychildren(v);
});
});
}
});
我面对的问题是displaychildren函数中的onclick函数不起作用。如果onclick条件被删除,所有组件的所有元素都将显示出来。我只想显示所选组件的子项。有谁知道是什么原因造成的问题
2013-04-09
Praneeth