javascript jsTree - 在 loaded.jstree 事件上获取选定的节点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8037176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 02:04:18  来源:igfitidea点击:

jsTree - Get the selected node on loaded.jstree event

javascriptjqueryjstree

提问by Yair Nevet

How can I get the selected node on the loaded.jstree event?

如何在loaded.jstree 事件上获取选定节点?

what should I do in the event handler:

我应该在事件处理程序中做什么:

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

By the way, I found out that the event data arg object contains a function called get_selected() but couldn't get anything from it.

顺便说一下,我发现事件数据 arg 对象包含一个名为 get_selected() 的函数,但无法从中获取任何信息。

My purpose is to redirect the client to the current selected node (by 'url' attribute).

我的目的是将客户端重定向到当前选定的节点(通过 'url' 属性)。

Thanks in advance

提前致谢

采纳答案by Guillaume Cisco

Seems according to the documentation of the demo here :

似乎根据此处的演示文档:

http://www.jstree.com/demo

http://www.jstree.com/demo

you can do :

你可以做 :

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

or

或者

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

Read carefully the documentation as :

仔细阅读文档如下:

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

一个被使用,这是因为如果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(); })

For the last event delegate, instead of writing event.preventDefault();, you can make your redirection correctly if you're not using the UI plugin, and write : window.location = $(this).attr('href');

对于最后一个事件delegateevent.preventDefault();如果您不使用 UI 插件,您可以正确地进行重定向,而不是编写:window.location = $(this).attr('href');

回答by Himanshu Pathak

you can select current node by :

您可以通过以下方式选择当前节点:

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

Code becomes:

代码变为:

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