javascript 删除jsTree中的所有节点

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

Delete all nodes in jsTree

javascriptjqueryjstree

提问by Jeff Dege

Is there a way to clear out all nodes from a jsTree that's faster than walking through all the nodes deleting them one-by-one?

有没有办法从 jsTree 中清除所有节点,这比遍历所有节点一个一个地删除它们更快?

采纳答案by Adam Terlson

See the documentation here: http://www.jstree.com/documentation/core

请参阅此处的文档:http: //www.jstree.com/documentation/core

.delete_node ( node )

Removes a node. Triggers an event.

mixed node

This can be a DOM node, jQuery node or selector pointing to the element you want to remove.

.delete_node(节点)

删除一个节点。触发一个事件。

mixed node

这可以是指向要删除的元素的 DOM 节点、jQuery 节点或选择器。

It seems you can just do a selector that will delete all the nodes you want, no loops required.

似乎你可以做一个选择器来删除你想要的所有节点,不需要循环。

回答by Brad

The simplest way I have found is to simply call .emptyon the div containing the tree.

我发现的最简单的方法是简单地调用.empty包含树的 div。

$('#tree').empty();

You might choose to use a more specific selector as a parameter for empty(), but this works fine for me.

您可能会选择使用更具体的选择器作为 的参数empty(),但这对我来说很好用。

回答by user602599

$('#tree').jstree("destroy").empty();

This is what worked for me. First destroy jstree elements and associated events, and then empty the div containing jstree.

这对我有用。首先销毁jstree元素和关联事件,然后清空包含jstree的div。

回答by Bruno Lafont

myTree.delete_node(myTree.get_node("#").children);

回答by mikerobi

Call .remove(node)on the root nodes.

调用.remove(node)根节点。

回答by dnshio

The following call will destroy the current instance of jsTree, remove any bound event listeners and obviously achieve your ultimate goal of removing all nodes. But this method is a bit of a over-kill, it has to be said.

以下调用将销毁 jsTree 的当前实例,删除所有绑定的事件侦听器,显然实现了删除所有节点的最终目标。但是这个方法有点过头了,不得不说。

$("#DivElementContainingYourTree").jstree("init");

回答by Ravi Khambhati

$('#jstree').parent().html('<div id="#jstree"></div>');