2010-09-09 32 views
20

我正在使用jsTree jQuery插件,並希望在用戶雙擊節點時執行代碼。如何將自定義行爲附加到jsTree中雙擊?

我似乎無法得到它的工作。我在ondblclk事件中發現了一些文檔,但它不會觸發。

browser.jstree(
      { 
       plugins: ["themes", "json_data", "ui", "cookies"], 
       callback: 
       { 
        ondblclk: function (node, tree) { 
         if (!thisReportBrowserthis._isFoldersOnly) { 
          var f = node; 
         } 
        } 
       } 
      } 
     ); 

我如何處理與jstree雙擊事件?

+1

看來我發現上面的代碼片斷文件是出於過期。 – GiddyUpHorsey 2010-09-10 00:00:37

回答

21

原來我可以這樣做:

jstree.bind("dblclick.jstree", function (event) { 
    var node = $(event.target).closest("li"); 
    var data = node.data("jstree"); 
    // Do my action 
}); 

node包含被點擊並data包含了我的它信息的元數據的li

+1

數據返回未定義。 – 2016-11-14 01:53:32

6

'dblclick.jstree'在最新版本的jsTree 1.0中不存在。

的DFP節點:

$("#yourtree").delegate("a","dblclick", function(e) { 
    var idn = $(this).parent().attr("id").split("_")[1]; 
    alert(idn); //return NodeID  
}); 

插入此,如果你只想dblclicked節點

if (this.className.indexOf('icon') == -1) { /* is the node clicked a leaf? */ } 
+0

我的樹上沒有看到任何「id」(我使用HTML_DATA插件創建我的樹,但我沒有打算創建任何ID),但忽略了這些代碼的工作原理。 「.on()」是更現代的jQuery方式,所以做$(「#yourtree」)。on(「dblclick」,「a」,function(e){...}); – mhenry1384 2012-07-01 17:10:33

4

這是不同的走出我的數據位,但在其他方面GiddyUpHorsey的回答得很正確。這裏是再次驗證碼:

 jstree.bind("dblclick.jstree", function (e, data) { 
      var node = $(e.target).closest("li"); 
      var id = node[0].id; //id of the selected node 
     }); 
+0

與我相同,除了從回調中刪除數據,因爲它是未定義的 – frage 2017-08-10 00:08:21

1

以上答案不要jstree的最新版本的工作(這是3.3.4)
這花了我心靈的一天彎曲的工作,但我終於得到它。 這裏正在雙擊編輯代碼:

$('#tree1').bind("dblclick.jstree", function (event) { 
 
    var tree = $(this).jstree(); 
 
    var node = tree.get_node(event.target); 
 
    tree.edit(node); 
 
});

,這裏是一個工作jsfiddle

0

爲3.3.5版本,我用這一個:

 $('#jstree').on("dblclick.jstree", function (e) { 
      var instance = $.jstree.reference(this), 
      node = instance.get_node(e.target); 
      // Code 
     }); 
相關問題