javascript JsTree 点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14880491/
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 22:50:04  来源:igfitidea点击:

JsTree Click Event

javascriptjstree

提问by Jerry Meng

Hi I am new to JsTree and I got a question on event. Let's call the tree structure stuff as LeftSide and the content rendered after you click one node on the tree as RightSide. What I can do is that you click one node at LeftSide then there is a subpage will be rendered at RightSide.

嗨,我是 JsTree 的新手,我有一个关于活动的问题。让我们将树结构的东西称为 LeftSide,将在单击树上的一个节点后呈现的内容称为 RightSide。我可以做的是,您单击 LeftSide 的一个节点,然后将在 RightSide 呈现一个子页面。

Now, what I need is to click something on the RightSide (This "something" can be a node name on the LeftSide) and it will render a subpage on the RightSide as same as if you click that node on the LeftSide.

现在,我需要单击 RightSide 上的某些内容(此“某些内容”可以是 LeftSide 上的节点名称),它将在 RightSide 上呈现一个子页面,就像您单击 LeftSide 上的该节点一样。

I am not sure whether I make my question clear.

我不确定我是否把我的问题说清楚了。

The point is that I don't know how to describe my question and search it one the web. So if you don't know the exact answer, you you just tell me which direction I should search for it. (If you tell me to read the official document, please specify which part rather than the whole doc.)

关键是我不知道如何描述我的问题并在网上搜索它。所以如果你不知道确切的答案,你只需告诉我我应该寻找哪个方向。(如果你告诉我阅读官方文档,请说明是哪一部分而不是整个文档。)

Appreciate for any help!!!

感谢您的帮助!!!

采纳答案by Pete Kirkham

You want to listen for the select_node.jstreeevent, and get the selected node(s). You can then do what you want to the right side of your page.

您想侦听select_node.jstree事件,并获取选定的节点。然后,您可以在页面右侧执行您想要的操作。

$("#jstree_id").on('select_node.jstree', function(e) {
    // gather ids of selected nodes
    var selected_ids = [];
    $("#jstree_id").jstree('get_selected').each(function () { 
        selected_ids.push(this.id); 
    }); 
    // do summit with them
    $('#RightSide').text('selected '+selected_ids);
});

Edited as bindhas been deprecated in favour of on.

编辑为bind已弃用,支持on.

回答by Rocky Hu

I don't think the "select_node.jstree" event is the best solution, because when you refresh the tree, it also will be fire. The below snippet code is the way that I use:

我不认为“select_node.jstree”事件是最好的解决方案,因为当你刷新树时,它也会被触发。下面的代码片段是我使用的方式:

$("#categories_tree").on("click", ".jstree-anchor", function(e) {
   var id = $("#categories_tree").jstree(true).get_node($(this)).id;
}