簡體   English   中英

jsTree-在load.jstree事件中獲取選定的節點

[英]jsTree - Get the selected node on loaded.jstree event

如何在loaded.jstree事件中獲取選定的節點?

我應該在事件處理程序中做什么:

    $('#Tree').bind('loaded.jstree', function(event, data){//TODO: How to get the selected node here?}).jstree();

順便說一下,我發現事件數據arg對象包含一個名為get_selected()的函數,但無法從中獲取任何信息。

我的目的是將客戶端重定向到當前選定的節點(通過“ url”屬性)。

提前致謝

似乎根據此處的演示文檔:

http://www.jstree.com/demo

你可以做 :

.one("reselect.jstree", function (event, data) { });

要么

.bind("select_node.jstree", function (event, data) {  
                // `data.rslt.obj` is the jquery extended node that was clicked 
                alert(data.rslt.obj.attr("id")); 
            })

請仔細閱讀以下文檔:

使用一個是因為這是因為如果調用refresh則會觸發這些事件

// 1) if using the UI plugin bind to select_node
        .bind("select_node.jstree", function (event, data) { 
            // `data.rslt.obj` is the jquery extended node that was clicked
            alert(data.rslt.obj.attr("id"));
        })
        // 2) if not using the UI plugin - the Anchor tags work as expected
        //    so if the anchor has a HREF attirbute - the page will be changed
        //    you can actually prevent the default, etc (normal jquery usage)
        .delegate("a", "click", function (event, data) { event.preventDefault(); })

對於最后一個事件delegate ,而不是編寫event.preventDefault(); ,如果您不使用UI插件,則可以正確進行重定向,然后輸入: window.location = $(this).attr('href');

您可以通過以下方式選擇當前節點:

$('#' + data.node.id)

代碼變為:

$('#Tree').bind('loaded.jstree', function(event, data){
console.log($('#' + data.node.id)); //This is current node, see on console
}).jstree();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM