单击按钮时,在jstree中按ID删除节点 [英] Remove node by ID in jstree when button is clicked
本文介绍了单击按钮时,在jstree中按ID删除节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 jstree ,我希望在点击按钮后按ID删除特定节点。
I'm using jstree and I want to delete a specific node by its ID after a click on a button.
这是我的树以html列表格式:
This is my tree in html list format:
<div id="testtree">
<ul>
<li id="1" title="ID:1"><a>Fruits and Vegetables</a>
<ul>
<li id="11" title="ID:11"><a>Fruit</a>
<ul>
<li id="111" title="ID:111"><a>Apple</a></li>
<li id="112" title="ID:112"><a>Banana</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
这是我的按钮事件(我有几个按钮,因此数组):
and this is my button event (I've got several buttons, hence the array):
buttons[0].addEventListener( "click", function( ev ) {
$("#testtree").jstree("remove", $("111"));
});
我缺少什么想法?
更新:
我已经纠正了错字但它仍然不起作用。这是完整的代码,可能是其他地方的错误?
I've corrected the typo but it still doesn't work. Here's the complete code, maybe the mistake is somewhere else?
<html>
<head>
<title>jstree test</title>
</head>
<body>
<div id="testtree">
<ul>
<li id="1" title="ID:1"><a>Fruits and Vegetables</a>
<ul>
<li id="11" title="ID:11"><a>Fruit</a>
<ul>
<li id="111" title="ID:111"><a>Apple</a></li>
<li id="112" title="ID:112"><a>Banana</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<button>Remove Apple</button>
<script type="text/javascript" src="_lib/jquery.js"></script>
<script type="text/javascript" src="jquery.jstree.js"></script>
<script>
$(document).ready(function() {
$("#testtree").jstree({
"plugins" : [ "themes", "html_data", "checkbox", "ui" ],
"core": { "initially_open": ["1"]}
});
});
var buttons = document.querySelectorAll("button");
buttons[0].addEventListener( "click", function( ev ) {
$("#testtree").jstree("remove","#111");
});
</script>
</body>
</html>
推荐答案
根据jsTree 文档
$("#testtree").jstree("remove","#111");
没有 $()
$("#testtree").jstree({
"plugins": ["themes", "html_data", "checkbox", "ui", "crrm"],
"core": {
"initially_open": ["1"]
}
});
您需要在插件中添加crrm
You need to add "crrm" to plugins
这篇关于单击按钮时,在jstree中按ID删除节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文