我正在尝试向jsTree块添加一个新节点,但不知何故我的代码无效。我已经尽了一切努力,但仍然没有找到任何解决方案,为什么它不起作用。
HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jsTree test</title>
<!-- 2 load the theme CSS file -->
<link rel="stylesheet" href="dist/themes/default/style.min.css" />
</head>
<body>
<!-- 3 setup a container element -->
<div id="jstree">
<!-- in this example the tree is populated from inline HTML -->
<ul>
<li>Root node 1
<ul>
<li id="child_node_1">Child node 1</li>
<li>Child node 2</li>
</ul>
</li>
<li>Root node 2</li>
</ul>
</div>
<button onclick="addLeaf()">Add Leaf</button>
<!-- 4 include the jQuery library -->
<script src="jquery/1.12.1/jquery.min.js"></script>
<!-- 5 include the minified jstree source -->
<script src="dist/jstree.min.js"></script>
<script>
$(function () {
// 6 create an instance when the DOM is ready
$('#jstree').jstree();
});
</script>
<script src="main.js"></script>
</body>
</html>
JavaScript的:
function addLeaf() {
var position = 'inside';
var parent = $('#jstree').jstree('get_selected');
var newNode = { state: "open", data: "mydata" };
$('#jstree').jstree("create_node", parent, position, newNode, false, false);
}