Javascript 从json';使用JQuery.getJSON()的
我需要从json文件加载jsTree的contextmenu。contextmenu保存在此文件(“test.json”)中: 加载contextmenu的代码是:Javascript 从json';使用JQuery.getJSON()的,javascript,jquery,json,jstree,Javascript,Jquery,Json,Jstree,我需要从json文件加载jsTree的contextmenu。contextmenu保存在此文件(“test.json”)中: 加载contextmenu的代码是: $(function () { $("#tree").jstree({ "plugins" : [ "themes", "json_data", "ui", "contextmenu" ], // other code .... "contextmenu" : {
$(function () {
$("#tree").jstree({
"plugins" : [ "themes", "json_data", "ui", "contextmenu" ],
// other code ....
"contextmenu" : {
"items" : customMenu
}
})
});
function customMenu(node) {
$.getJSON( "test.json", function(json) {
return json;
});
}
这样,我看不到上下文菜单。你能帮助我吗?我不知道jstree插件是如何工作的,但也许你应该尝试另一种方法,首先加载JSON数据,发出Ajax请求,完成后,初始化jstree:
$(function () {
$.getJSON( "test.json", function(json) {
$("#tree").jstree({
"plugins" : [ "themes", "json_data", "ui", "contextmenu" ],
"contextmenu" : {
"items" : json
}
});
});
});
这是因为Ajax调用是异步的,所以您的
customMenu()
函数没有向“contextmenu”
的“items”
选项返回任何内容。Ajax调用不是异步的还是同步的?
$(function () {
$.getJSON( "test.json", function(json) {
$("#tree").jstree({
"plugins" : [ "themes", "json_data", "ui", "contextmenu" ],
"contextmenu" : {
"items" : json
}
});
});
});