以编程方式将子节点添加到jstree

use*_*766 13 javascript jstree

我正在尝试编写一些动态地将节点添加到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>
Run Code Online (Sandbox Code Playgroud)

WSk*_*kid 13

在ID中使用句点时,您需要像这样转义它们:

$("#tree").jstree("create", $("#child1\\.id"), "inside",  { "data" : "child2" },
                          function() { alert("added"); }, true);
Run Code Online (Sandbox Code Playgroud)

这是因为它使用jQuery选择器.在jsTree FAQ中提到了这里:http: //www.jstree.com/faq/

  • 在答案中链接到FAQ已经死了. (2认同)