如何在 loaded.jstree 事件中获取选中的节点?
我应该在事件处理程序中做什么:
$('#Tree').bind('loaded.jstree', function(event, data){//TODO: How to get the selected node here?}).jstree();
顺便说一下,我发现事件数据 arg 对象包含一个名为 get_selected() 的函数,但无法从中获取任何信息。
我的目的是将客户端重定向到当前选定的节点(通过“url”属性)。
提前致谢
最佳答案
似乎根据此处演示的文档:
你可以这样做:
.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/