如果我的jtree初始化函数与调用者在同一个文件中,一切都可以工作,但如果我将函数调用移动到html文档,它不会返回任何东西。下面是我到目前为止编写的代码:
function cattree(treeID, treeAncestors, treeCurrent) {
alert(treeID + treeAncestors + treeCurrent);
$("#"+treeID).jstree({
"core" : {
"initially_load" : treeAncestors,
},
"ui" :{
"select_limit" : 1,
"initially_select" : [treeCurrent],
},
"json_data" : {
"progressive_render" : true,
"progressive_unload" : true,
"ajax" : {
"url" : "/taxonomy/catjson",
"dataType": "text json",
"data" : function (n) {
return { id : n.attr ? n.attr("id") : 0 };
}
}
},
"plugins" : [ "themes", "json_data", "ui" ],
});
}
当我将它移动到文档时,我认为它不会等待带有函数的文档加载。
$(document).ready(function(){
cattree("demo", ['4e974c91f0282e7011000004', '4e974d92f0282ec41a00000a'], ['4e974da1f0282e2c0c000000']);
}
);
结果如下:
cattree init在外部文件
(function () {
if(jQuery && jQuery.jstree && jQuery.cattree) { return; }
(function ($) {
$.cattree = function(treeID, treeAncestors, treeCurrent) {
//alert(treeID + treeAncestors + treeCurrent);
//tree initialization function to possibly allow one or more tree on page.
$("#"+treeID).jstree({
"core" : {
"initially_load" : treeAncestors,
},
"ui" :{
"select_limit" : 1,
"initially_select" : treeCurrent,
},
"json_data" : {
"progressive_render" : true,
"progressive_unload" : true,
"ajax" : {
"url" : "/taxonomy/catjson",
"dataType": "text json",
"data" : function (n) {
return { id : n.attr ? n.attr("id") : 0 };
}
}
},
"plugins" : [ "themes", "json_data", "ui" ],
});
}
})(jQuery);
})();
head部分的代码或者在所有html下面的代码:
$(document).ready(
function(){
$.cattree("demo", ['4e974c91f0282e7011000004', '4e974d92f0282ec41a00000a'], ['4e974da1f0282e2c0c000000']);
}
);
目前它工作得很好。我认为问题在于函数是在所有东西加载之前被调用的。和之前一样添加Jquery。如果语句无法识别$。