實現思路: 1、ztree中節點是透過一個json的Array作為資料來源、,所以直接操作json字串,然後轉為json物件陣列。 2、然後用新構成的json陣列物件重新初始化ztree物件。 複製程式碼 程式碼如下: <br> var zTreeObj1;<br> var zTreeObj2;<br> var leftDivStr = "[";<br> var rightDivStr = "[";<br> var setting = {<br> edit: {<br> enable: false,<br> showRemoveBtn: false,<br> showRenameBtn: false<br> },<br> data: {<br> simpleData: {<br> enable: true<br> }<br> },<br> callback: {<br> //onClick : menuOnClick<br> }<br> };<br> function menuOnClick(event, treeId, treeNode, clickFlag) {<br><br> }<br> //注册toSource函数,解决ie不支持Array的toSource()方法的问题<br> Array.prototype.toSource = function (){<br> var str = "[";<br> for(var i = 0 ;i<this.length;i++){<br> str+="{id:\""+this[i].id+<br> "\",pId:\""+this[i].pId<br> +"\",name:\""+this[i].name<br> +"\",isParent:\""+this[i].isParent<br> +"\",file:\""+this[i].file<br> +"\",icon:\""+this[i].icon<br> +"\",open:\""+this[i].open<br> +"\"},";<br> }<br> if(this.length != 0){<br> str = str.substring(0, str.length-1);<br> }<br> str +="]";<br> return str; <p> } ;<br> //注册unique函数,去掉array中重复的对象(id相同即为同一对象)<br> Array.prototype.unique = function (){<br> var r = new Array();<br> label:for(var i = 0, n = this.length; i < n; i++) {<br> for(var x = 0, y = r.length; x < y; x++) {<br> if(r[x].id == this[i].id) {<br> continue label;<br> }<br> }<br> r[r.length] = this[i];<br> }<br> return r;</p> <p> } ;<br> //初始資料<br> var zNodes =[<br> var zNodes =[<br> var zNodes =[<br> var zNodes =[<br> true},<br> { id: 11, pId:1, name:"葉子節點1-1",open:true},<br> { id:111, pId:11, "葉 112, pId:11, name:"葉節點11-2"},<br> { id:12, pId:1, name:"葉節點1-2 121, pId:12, name:"葉子節點12-1"},<br> { id:122, pId:12, name:"葉子木1, name:"葉子節點1-3"},<br> { id:2, pId:0, name:"父節點2", open:true}, name:"葉子節點2-1"},<br> { id:22, pId:2, name:"葉子節點2-2"},<br> 節點2-3"},<br> { id:3, pId:0, name:"父節點3", open:true},<br> -1"},<br> { id:32, pId:3, name:"葉子節點3-2"},<br> <br> ];<br><br> for(var i=0;i<znodes.length i znodes> }<br> }<br> length-1);<br> leftDivStr ="]";<br> rightDivStr = "]"; parentNodes = "{id:" treeNode.id ",pId:" treeNode.pId <br> if(treeNode != null && treeNode.getParentNode()!= null){<br> parentNodes }<br> return parentNodes;<br> } <br> //移動節點<br> function moveNodes(zTreeFrom,treeNode,zTreeTo,divStr // //////////treeNode所有的父節點<br> var parentNodes ="[";<br> parentNodes = findParentNodes(treeNode,parentNodes); <br> parentNodes = parentNodes.substring(0,parentNodes.length-1); <br><br> ParentNodes ="]";<br> Nodes);<br> //////////////// / /////////////////<br> var Nodes = "[";<br> , name:"" treeNode.name "",open:" treeNode.open "},";<br> Nodes = operChildrenNodes(treeNode,nodes) > ) ;<br> 節點="]";<br> var NodesArray = eval(nodes); var divToArray = eval(divStrTo);<br> for(var i = 0;i<nodesarray.length> for(var j = = 0;j<from if i> divFromArray.splice(j,1) }<br> ,<br> divToArray = divToArray.concat(parentNodesArray);<br><br> /// // ////////////////////////////////////////////// ///////////////////////////////////////////////去重複<br> divFromArray = divFromArray.unique();<br> ///// ///////////// /// //////////////////////////////////////////// /////// //////////////// //////////////////////去重複<br><br> if(zTreeFrom. setting.treeId == "treeDemo"){<br> leftDivStr = divFromArray .toSource, 🎜> $.fn.zTree.init($ ("#treeDemo") , 設定, divFromArray);<br> $.fn.zTree.init ($("#treeDemo2"), > leftDivStr = divToArray.toSource(); <br> 🎜> $.fn.zTree. init($("# treeDemo"), 設定,divToArray);<br> }<br> }<br><br> <br> //與所有子節點尋找指定節點下的所有子節點<br> if(treeNode.children!= undefined){//是父節點,有子點<br> 為(var j = 0;j<treenode.children.length> tree <br> nodes ="{id:" childNode.id " ,pId:" childNode.pId ",name:"" childNode.name "",open:" childNode.open "},";<br> }<br> } else{//沒子節點<br> }<br> > $(document).ready(function(){<br> zTreeObj1 = $.fn. zTree.init($("#treeDemo"), setting, zNodes);<br> zTreeObj2 = $.fn.zTree.init($("#treeDemo2"), setting); ) { <br> $("#toRight").click(function() {<br> Nodes()[0],zTreeObj2,leftDivStr,rightDivStr);<br> }); moveNodes(zTreeObj2,zTreeObj2.get),Divj2.getSelected. 🎜> }); <br> "#show").click(function(){<br> var leftDivStrArray = eval(leftDivStr);<br> i ){<br> leftDiv[i] = }<br> var rightDiv = new Array();<br> i = 0;i<rightdivstrarray.length> }<br> alert(leftDiv);<br> alert(rightDiv);<br><br> }); <br> });<br> });<br><br> <br> <br></rightdivstrarray.length></treenode.children.length></from></nodesarray.length></znodes.length></p> </div> <p>html代碼:</p> <p></p> <div class="codetitle"> <span><a style="CURSOR: pointer" data="6981" class="copybut" id="copybut6981" onclick="doCopy('code6981')"><u>複製程式碼</u></a></span>程式碼如下:</div> <div class="codebody" id="code6981"> <br><div class="content_wrap"> <br> <div class="zTreeDemoBackground left"> <br> <ul id="treeDemo ><BR> <按鈕id=" toright>>><br> <br> ><br> <div class="right"> <br> <ul id="treeDemo2" class="ztree"></ul> <br> </div> <br> </ul> </div> <br> </div> </div> <p></p>