我正在尝试编写一些代码来动态地将节点添加到 jstree。我关注了 http://www.jstree.com/documentation/crrm 的文档但不能得到一个简单的例子来工作——节点 child2 被添加,但它被添加到节点“root.id”而不是指定的“child1.id”...任何提示将不胜感激.代码如下
<html>
<head>
<script type="text/javascript" src="http://static.jstree.com/v.1.0rc2/jquery.js"></script>
<script type="text/javascript" src="http://static.jstree.com/v.1.0rc2/jquery.jstree.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$(function () {
$("#tree").jstree({
"json_data" : {
"data" : [
{
"data" : "parent",
"attr" : { "id" : "root.id" },
"children" : [ { "data" : "child1",
"attr" : { "id" : "child1.id" },
"children" : [ ] }
]
},
]
},
"plugins" : [ "themes", "json_data", "crrm" ]
});
});
$("#add").click(function() {
$("#tree").jstree("create", $("#child1.id"), "inside", { "data" : "child2" },
function() { alert("added"); }, true);
});
});
</script>
</head>
<body>
<div id="tree" name="tree"></div>
<input type="button" id="add" value="add" />
</body>
</html>
最佳答案
在 ID 中使用句点时,您需要像这样转义它们:
$("#tree").jstree("create", $("#child1\\.id"), "inside", { "data" : "child2" },
function() { alert("added"); }, true);
这是因为它使用 jQuery 选择器的方式。它在位于此处的 jsTree 常见问题解答中提到: http://www.jstree.com/faq/
关于javascript - 以编程方式将子节点添加到 jstree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3479715/