使用 JSTree 的 check_callback 函数。它传递了这些论点
operation, node, parent, position, more 可用于获取您需要的信息。 position 返回一个数字,0 表示悬停,如果被丢弃将产生一个孩子,1 表示兄弟姐妹。以下是更多文档的链接:
http://www.jstree.com/api/#/?q=check_call&f=$.jstree.defaults.core.check_callback
https://github.com/vakata/jstree#more-on-configuration
$("#tree").jstree({
"core" : {
"check_callback" : function (operation, node, parent, position, more) {
if(operation === "copy_node" || operation === "move_node") {
if(parent.id === "#") {
return false; // prevent moving a child above or below the root
}
},
return true; // allow everything else
}
},
"plugins" : ["dnd","contextmenu"]
});
为了更好地理解输出,将参数传递给控制台:
$("#tree").jstree({
"core" : {
"check_callback" : function (operation, node, parent, position, more) {
console.log(operation, node, parent, position, more);
},
return true;
}
},
"plugins" : ["dnd"]
});