当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Unable to add new node in jsTree

问题描述:

I am trying to add a new node to a jsTree block, but somehow my code is not working. I have already tried everything I could but still have not found any solution to why it is not working.

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);

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: