本文介绍了jsTree:如何获取选定节点的ID到jsTree中的根节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 如何获取选定节点的ID到jsTree中的根节点? 假设C是选定节点,那么如何获得C的所有父ID? A B 使用父母获取所有父母, li ,因为 jstree 中的所有树项都是 li ,请尝试这: var parents = data.rslt.obj.parents(li); 对于jQuery中的孩子使用 children 像这样: var children = data.rslt.obj.parent()。find('li'); 编辑使用上面的,这里是如何让所有的父母和孩子,并把它们放在所有每一个数组: 家长: $ b var parents = []; data.rslt.obj.parents(li)。each(function(){ parents.push({id:$(this).attr(id),description:$(这个).children(a).text()}); }); 子女: var children = []; data.rslt.obj.find(li)。each(function(){ children.push({id:$(this).attr(id),description:$(这个).children(a).text()}); }); How to get IDs of selected nodes to root node in jsTree?Assume C is selected node then How can I get All parent IDs of C.ABC+C1+c2Following code will return only immediate parent ID:If I selected C then I get only B .bind("select_node.jstree", function (event, data) { //`data.rslt.obj` is the jquery extended node that was clicked alert("Selected node = "+ data.rslt.obj.attr("id")); alert("Parent of Selected node = "+ data.inst._get_parent(data.rslt.obj).attr("id")) });Output:Selected node = CParent of Selected node = BIs there any way to get all parent nodes ID i.e. Selected node to root node ?How to get all child nodes of selected node in jsTree ?Any help or guidance in this matter would be appreciated. 解决方案 Use parents in jQuery to get all parents, filtering out by li because all tree items are li in jstree, try this:var parents = data.rslt.obj.parents("li");And for children use children in jQuery, like so:var children = data.rslt.obj.parent().find('li');EDIT Using the above, here's how to get all parent and children and put them in all an array for each:Parents:var parents = [];data.rslt.obj.parents("li").each(function () { parents.push({ id: $(this).attr("id"), description: $(this).children("a").text() });});Children:var children = [];data.rslt.obj.find("li").each(function () { children.push({ id: $(this).attr("id"), description: $(this).children("a").text() });}); 这篇关于jsTree:如何获取选定节点的ID到jsTree中的根节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!