javascript 获取jstree子节点的直接父节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12914841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:26:28  来源:igfitidea点击:

Get the immediate parent of child node in jstree

javascriptjstree

提问by user1596278

I want to retrieve the parent of the child nodewithout clicking on the tree..

我想在child node不点击tree..

data.inst._get_parent(data.rslt.obj).attr("id");

Above command give me the immediate parent when we click on the child nodes.

当我们点击child nodes.

Is there a way to get the parent nodewithout clicking on the child node.

有没有办法在parent node不点击child node.

Regards, Praque M

问候, 普拉克 M

回答by Dane W

It seems the "data.inst" was renamed in newer versions to "data.instance". This made tracking down the solution difficult

似乎“data.inst”在较新版本中已重命名为“data.instance”。这使得追踪解决方案变得困难

data.instance.get_parent(data.node)returns the string ID of the parent (much unexpected for me). To get the parent I had to call data.instance.get_node()on the string ID.

data.instance.get_parent(data.node)返回父级的字符串 ID(对我来说很意外)。为了获得父级,我必须调用data.instance.get_node()字符串 ID。

data.instance.get_parent(data.node)is also accessible thru data.node.parent.

data.instance.get_parent(data.node)也可以通过 data.node.parent 访问。

Example:

例子:

$('#MaterialCollectionTree').on('activate_node.jstree', function(e, data) {
  if(data.instance.is_leaf(data.node)) {
    alert("Leaf: " + data.node.text);
    alert("Parent: " +  data.instance.get_node(data.node.parent).text);
  }
});

回答by user1401768

It is a bit more complex then that

那就有点复杂了

parent_node = $.jstree._reference('#tree_id')._get_parent(n);

The variable parent_node is a jquery object so the command

变量 parent_node 是一个 jquery 对象,所以命令

parent_node.attr("something");

is the same as

是相同的

$("#parent_node_id").attr("something");