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

标签 javascript jquery 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(); })

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

关于javascript - jsTree - 在 loaded.jstree 事件上获取选定的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8037176/

相关文章:

javascript - requestAnimFrame 在 Chrome 中为 60FPS,在 FF 中为 1FPS

javascript - 用户对 Sentry 报告对话框未提交的反馈

javascript - 在测验结束时显示用户答案

javascript - jQuery .on 事件不适用于变量选择器

javascript - 从 jstree 中的外部 json 文件加载数据

javascript - 如何将 try 声明的范围也限制在 finally block 中?

javascript - 在 Outlook 365 加载项中捕获 "onChange"事件

jquery-ui - 什么是 jQuery UI 替代具有 500,000 个选项的选择框?

javascript - 过滤包含多个单词的过滤列表

jquery - 从 html 数据中选中的 jsTree 复选框