2012-05-08 52 views
0

我必須獲取點擊節點下的每個子節點的名稱。假設我發送點擊節點的函數爲:獲取所有子節點的子名稱

getNodes(node){ 
$(node).children().each(function() { 
    //want child nodes name under the sent node(node) 
}); 
} 

如何獲得此工作?

回答

1

我一直在努力尋找一種方法。這裏是我想出的:

而不是使用jsTree,我使用jQuery選擇器。這很好,因爲每個節點的元數據都存儲在jQuery數據中。

這是我如何去一下吧:

$("#treeDiv").jstree({ 
     "json_data":{data:jsTree}, 
     "themes":{ 
      "theme":"classic", 
      "dots":false, 
      "icons":false 
     }, 
     "plugins":[ "themes", "json_data", "ui" ] 
    }).bind("select_node.jstree", 
       function (e, data) { 
        $(data.rslt.obj).find("li").each(function(idx, listItem) { 
         console.log($(listItem).data("name")); 
        }); 
       }); 

這將打印在控制檯中的所有名稱。假設你已經將它們添加爲元數據。請參閱jsTree文件的詳細信息,元數據:http://www.jstree.com/documentation/json_data

第二個選擇是更換$(listItem).data("name")通過$(listItem).find("a").text()

這很好地工作只爲最後一級爲葉有他們的標記名稱,但父母將打印更多。

希望這有助於。

相關問題