这是我的 xml 文件:-
<root>
<child1 entity_id = "1" value= "Asia">
<child2 entity_id = "2" value = "india">
<child3 entity_id = "3" value = "Gujarat">
<child5 entity_id = "5" value ="Rajkot"></child5>
</child3>
<child4 entity_id = "4" value = "Rajshthan">
<child6 entity_id = "6" value = "Ajmer"></child6>
</child4>
</child2>
</child1>
</root>
这是我的代码:-
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="jquery.js"></script>
<script>
data = false;
loaded = false;
function loadChild(id) {
if(!loaded) {
ul = "<ul>";
$(data).find("[entity_id='" + id + "']").children().each(function(){
var value_text = $(this).attr('value');
var id = $(this).attr('entity_id');
ul += "<li id='" + id + "'>" + value_text + "</li>";
});
ul += "</ul>";
$("#" + id).append(ul);
loaded = true;
}
}
$(function() {
$('#update-target a').click(function() {
$.ajax({
type: "GET",
url: "final.xml",
dataType: "xml",
success: function(xml) {
data = xml;
//console.log(data);
$(xml).find('child1').each(function(){
var value_text = $(this).attr('value');
var id = $(this).attr('entity_id');
$("<li id='" + id + "' onclick=\"loadChild('" + id + "');\"></li>")
.html(value_text)
.appendTo('#update-target ol');
}); //close each(
}
}); //close $.ajax(
}); //close click(
}); //close $(
</script>
</head>
<body>
<p>
<div id='update-target'>
<a href="#">Click here to load value</a>
<ol></ol>
</div>
</p>
</body>
</html>
我得到了这样的输出:-
亚洲 印度
我想要这样的输出:-
如果单击亚洲,则显示印度
如果单击印度,则显示古吉拉特邦和拉杰什坦
如果单击古吉拉特邦,则显示拉杰科特
如果单击拉杰什坦,则显示 Ajemr