如何从Java的XML中获取树状结构结构中的遍历路径 [英] how to get the traversing path in tree like structure loading from XML in Javascript
本文介绍了如何从Java的XML中获取树状结构结构中的遍历路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像下面这样的树结构.
I have a tree structure like below.
当我穿越父母到孩子时,我想要完整的路径,例如: 单击化学"->物理化学"->粒子论"->粒子论". 当我们返回或单击物理化学"时,路径将返回,如下所示: 化学->物理化学
when i travese through parent to child i want the full path like : Chemistry->Physical chemistry->Particle theory->Particle theory on clicking the particular node. and when we go back or click the Physical chemistry then path will return like that : Chemistry->Physical chemistry
我们该怎么做. 我正在使用以下功能.
how can we do that. i am using the following function.
function getParents(id)
{
var parents = $("#" + id).parents("ul");
var selector = "";
for (var i = parents.length-1; i >= 0; i--) {
//alert($(parents[i]).find('li').find('span').find('a').html());
selector += $(parents[i]).find('li').parent().find('span').find('a').html() + " > ";
}
//selector += "#" + id;
$('#breadcrumbs').html(selector);
//return selector;
}
但方向错误.
推荐答案
这是递归函数的主要示例.
This is a prime example for a recursive function.
function getBreadcrumbs(a) {
var path = $(a).text(),
$parent = $(a).parents("li").eq(1).find("a:first");
if ($parent.length == 1) {
path = getBreadcrumbs($parent) + " > " + path;
}
return path;
}
$("ul a").click(function () {
$("#breadcrumbs").text( getBreadcrumbs(this) );
});
在这里查看它的工作方式: http://jsfiddle.net/Tomalak/GFfyg/
See it working over here: http://jsfiddle.net/Tomalak/GFfyg/
这篇关于如何从Java的XML中获取树状结构结构中的遍历路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文