gpt4 book ai didi

jquery - JsTree Jquery 获取某一级别的所有节点

转载 作者:行者123 更新时间:2023-12-01 02:54:34 26 4
gpt4 key购买 nike

如何使用 jquery 获取某个级别的所有节点并为这些节点执行操作?我有一个 jstree,我用 json 数据填充,因此不可能为每个节点级别添加我自己的 id。

例如,我想获取第3层的所有节点:

Root
A1
1
2
3

数字 1、2 和 3 是 3 级节点。我想仅更改 3 级节点的图标。现在我可以使用以下内容更改所有节点图标:

a>.jstree-icon
{
background-image: url("content/img/usersmallclipart.png")!important;
background-position: 0!important;

}

我想使用 jquery 获取第 3 级的所有节点,并在满足特定上下文时使用 .css() 更改图标(这就是为什么我需要使用 jquery 而不是简单地深入了解我的CSS 样式)。

所以伪代码如下:

  1. 获取第 3 层的所有节点

  2. 如果节点有子节点 - 更改其图标,否则 - 不执行任何操作

我该怎么做?感谢任何帮助,谢谢!

最佳答案

TreeView 加载后,您可以使用 jQuery 迭代每个节点,并根据节点的级别应用 css。例如,一旦 jstree 加载 TreeView ,您可以尝试以下操作:

$("#myTree").bind('ready.jstree', function (event, data) {
var $tree = $(this);
$($tree.jstree().get_json($tree, {
flat: true
})).each(function () {
// Get the level of the node
var level = $("#myTree").jstree().get_node(this.id).parents.length;
var node;
if (level == 3) {
// node = ... apply desired css to the node here if it has children.
}
});
});

关于jquery - JsTree Jquery 获取某一级别的所有节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29025529/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com