jstree获取所选节点的级别
如何获得当前所选节点的学位/级别? 即它拥有的父母数量。
我有一节在选择节点时执行:
.bind("select_node.jstree", function (event, data) { var nodeInfo = $("#" + data.rslt.obj.attr("id")); console.log(data.rslt.obj.attr("id")); // the id of the node console.log(nodeInfo.children("a").text()); // the name of the node // the level of the node??? });
data.inst.get_path().length
1是根节点;
.bind("select_node.jstree",function(e,data) { var inst=data.inst; var level=inst.get_path().length; var selected=inst.get_selected(); var id=selected.attr('id'); var name=selected.prop('tagName'); console.log(name,id,level); });
(最好使用inst,而不是用dom搜索);
$(function () { $("#demo1").jstree({ "plugins" : [ "themes", "html_data", "ui", "cookies" ] }); $("#demo2").jstree({ "plugins" : [ "themes", "html_data", "ui", "cookies" ] }); $('#demo1,#demo2').bind("select_node.jstree",function(e,data) { var inst=data.inst; var level=inst.get_path().length; var selected=inst.get_selected(); var id=selected.attr('id'); var name=selected.prop('tagName'); console.log(name,id,level); }); });
With id-s: Without id-s:
你也可以使用(在jstree 3.1.0中):
.bind("select_node.jstree",function(e,data) { var level=data.node.parents.length; // 1 = root level, 2 = next level down, etc });
.bind("select_node.jstree", function(e, data) { var level = data.node.parents.length; var text = data.node.text; var id = data.node.id; console.log(level,text,id); }) ;