javascript – jsTree – 在loaded.jstree事件上获取所选节点

如何在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")); 
            })

仔细阅读文档:

one is used, this is because if refresh is called those events are
triggered

// 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(); })

对于最后一个事件委托,而不是编写event.preventDefault();,如果你没有使用UI插件,你可以正确地重定向,并写:window.location = $(this).attr(‘href’) ;

点赞