javascript 单击父级时展开 jsTree 节点

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

Expand jsTree node when parent is clicked

javascriptjquerycssjstree

提问by John 'Mark' Smith

I am trying to implement a very simple tree using jsTree. I have found the documentation dense and overwhelming.

我正在尝试使用 jsTree 实现一个非常简单的树。我发现文档密集且压倒一切。

Right now, I expand / collapse a node by clicking the arrow shown here:

现在,我通过单击此处显示的箭头展开/折叠节点:

enter image description here

在此处输入图片说明

I want to be able to expand / collapse by clicking the node name too:

我也希望能够通过单击节点名称来展开/折叠:

enter image description here

在此处输入图片说明

The code I am using is simple; I have not altered the javascript for jsTree:

我使用的代码很简单;我没有更改 jsTree 的 javascript:

<ul id="tree">
   <li>
      SubFolder1
      <ul id="tree">
         <li data-jstree='{"icon":"/Images/blue-folder.png"}'>Pub 1</li>
      </ul>
   </li>
</ul>

采纳答案by oerl

Just add an event listener in your html file and call the toggle_nodefunction. This code below listens for a single click.

只需在您的 html 文件中添加一个事件侦听器并调用该toggle_node函数。下面的这段代码侦听一次单击。

$(document).ready(function(){
  $('#jstree_div').on("select_node.jstree", function (e, data) { $('#jstree_div').toggle_node(data.node); });
}

If you want to listen for a double click you need another event listener, since jsTree does not support double click events yet.

如果您想侦听双击,则需要另一个事件侦听器,因为 jsTree 尚不支持双击事件。

$('#jstree_div').on("dblclick",function (e) { 
  var li = $(e.target).closest("li");
  var node = $('#jstree_div').get_node(li[0].id);

  $('#jstree_div').toggle_node(node)
});

Hope that helps.

希望有帮助。

回答by itmuckel

$('#tree').on('select_node.jstree', function (e, data) {
    data.instance.toggle_node(data.node);
});

That worked for me. oerls solution did not.

那对我有用。oerls 解决方案没有。

回答by Abdulaziz N

$('#jstree').on("select_node.jstree", function (e, data) { 
     $('#jstree').jstree("toggle_node", data.node);
 });

also this will work

这也行