gpt4 book ai didi

jquery - 如何获取jstree JSON数据中的节点属性

转载 作者:行者123 更新时间:2023-12-01 06:18:50 25 4
gpt4 key购买 nike

我想获取树中每个节点的属性。在网上查了一下,找到了一种方法,但是行不通。错误是(在 attr("description") 行:

Uncaught TypeError: Cannot read property 'obj' of undefined

这是我的代码:

jQuery(document).ready(function() {
var $ = jQuery;
$('#jstree').jstree({ 'core' : {
'data' : [
{"id":"parent","parent":"#","text":"parent"},
{"id":"cs","text":"Short Stay","parent":"parent","li_attr":{"label":"Short Stay","description":"example"}},
{"id":"ls","text":"ls","parent":"parent"},{"id":"cs_1","text":"cs_1","parent":"cs"},
{"id":"ls_1","text":"ls_1","parent":"ls"},{"id":"cs_1_1","text":"cs_1_1","parent":"cs_1"},
{"id":"cs_1_1_1","text":"cs_1_1_1","parent":"cs_1_1"},
{"id":"cs_1_1_2","text":"cs_1_1_2","parent":"cs_1_1"}
]
} })
.on("select_node.jstree",
function(evt, data){
$('#data').html(data.rslt.obj.attr("description"));
}
);
});

最佳答案

你只需要获取所选元素的id,然后获取该元素的属性:

$(function () {
$('#jstree').jstree({ 'core' : {
'data' : [
{"id":"parent","parent":"#","text":"parent"},
{"id":"cs","text":"Short Stay","parent":"parent","li_attr":{"label":"Short Stay","description":"example"}},
{"id":"ls","text":"ls","parent":"parent"},{"id":"cs_1","text":"cs_1","parent":"cs"},
{"id":"ls_1","text":"ls_1","parent":"ls"},{"id":"cs_1_1","text":"cs_1_1","parent":"cs_1"},
{"id":"cs_1_1_1","text":"cs_1_1_1","parent":"cs_1_1"},
{"id":"cs_1_1_2","text":"cs_1_1_2","parent":"cs_1_1"}
]
} }).on("select_node.jstree",
function(evt, data){
var node_id = (data.node.id); // element id
var description = $("#"+node_id).attr("description"); // get value of element attribute
$('#data').html(description);
}
);
});

请注意,并非所有元素都有 description 属性。

Fiddle

关于jquery - 如何获取jstree JSON数据中的节点属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36806414/

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