Dynatree-获取Div ID

时间:2014-04-09 07:47:33

标签: javascript jquery html

我在同一页面上有2个相同的dynatrees,如同js fiddle http://jsfiddle.net/37ppf/3/所示。

$("#tree1").dynatree({            
    onSelect : function(select,node){},             
    checkbox: true,           
    children: [ 
        {title: "Item 1",key:"1"},                
        {title: "Folder 2", isFolder: true, key:"2",
            children: [                        
                {title: "Sub-item 2.1", },                        
                {title: "Sub-item 2.2", }
            ]                
        },                
        {title: "Item 3",key:"5"}            
    ]       
});

$("#tree2").dynatree({            
    onSelect : function(select,node){},             
    checkbox: true,           
    children: [ 
      {title: "Item 1",key:"1"},                
      {title: "Folder 2", isFolder: true, key:"2",                   
        children: [                        
          {title: "Sub-item 2.1", },                        
          {title: "Sub-item 2.2", }                    
        ]                
      },                
        {title: "Item 3",key:"5"}            
    ]       
 });

<div id="tree1"> </div>
<div id="tree2"> </div>

现在,当用户选择任何dynatree中的任何节点时,我想获取div id,其中用户选择了该节点的树已加载。如果用户从第一棵树中选择,我想要获取输出为tree1(div id),如果从第二个树中选择一个节点,我想获得tree2。这可能吗。我试过了

$(this).closest(".dynatree-container").parent("div").attr("id")

但它的未定义。

2 个答案:

答案 0 :(得分:1)

感觉超级hacky,但你可以这样做:

onSelect : function(select,node){
    alert(node.tree.$tree[0].id);
}, 

node.tree。$ tree [0]将返回其父树的javascript对象。

请参阅

的工作代码

JSFiddle

答案 1 :(得分:0)

试试这个

$(document).on('click','span',function(){
  console.log($(this).parents('div').attr('id'))  
})

DEMO