ツリーのようなデータを持つjsonオブジェクトがあります。インタラクティブにUIに表示したいと思います。コンポーネントをクリックすると、その子が表示されます。次のコードを書きました
<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.9.1.js"></script>
</head>
<body>
<div>
<ul id="result"></ul>
</div>
<script>
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)
{
$('<li id="' + obj.component + '">').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){
$('<li id="' + v.component + '">').text(v.component + '-'+v.status).appendTo($result);
if$("#"+v.component).click(function(){
alert("Problem is here");
displaychildren(v);
});
});
}
});
私が直面している問題は、displaychildren 関数の要素の onclick が機能しないことです。onclick 条件が削除されると、すべてのコンポーネントのすべての要素が表示されます。選択したコンポーネントの子のみを表示したい。誰が問題の原因を知っていますか