2016-04-23 39 views
1

我想獲取樹中每個節點的屬性。在網上查找後,我找到了一種方法來做到這一點,但它不起作用。錯誤的是(在attr的線(「說明」):如何獲取jstree JSON數據中的節點屬性

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")); 
    } 
); 
    }); 
+0

您的意思是屬性值或屬性名稱? –

回答

5

你只需要獲得所選元素的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屬性