本文參考http://blog.csdn.net/m0_37355951/article/details/78004994?locationNum=4&fps=1javascript
用jstreel是由於公司要用一下無限極的樹結構;然後臺給的數據又不是同一種代碼風格css
代碼以下``` <!doctype html>html
<html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width"> <title>jstree 插件</title> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script> </head> <body> <div id="jstree"></div> <script type="text/javascript"> var isclick=true; $(function () { $('#jstree').jstree({ // 'plugins': ["checkbox", "state"], 'core': { 'data': [{ "id": "root", "text": "sxsf", "state": { //默認狀態展開 "opened": true }, "children": [] }], 'check_callback': true } }); }); java
function gettree(parentsid){ $.ajax({ type:"get", url:parentsid=="root"?"getFacultyListByUserIdAndCollegeIdLast":"getNextFacultyListById", async:true, data:{ tokenId:tokenId, userId:userId, collegeId:collegeId, id:parentsid }, success:function(res){ if(res.code==200){ var data=res; $.each((parentsid=="root"?data.body:data.body.list),function(index,obj){ obj.text=obj.name; createNode("#"+parentsid, obj.id, obj.name,"last"); }) isclick=true; } } }); } // API createNode(parent, id, text, position). // parent:在該節點下建立,id: 新節點id, text:新節點文本, position:插入位置 // 封裝一個函數動態建立節點 function createNode(parent_node, new_node_id, new_node_text, position) { debugger; if($("#"+new_node_id).length>0){//此處是爲了防止重複建立 }else{ $('#jstree').jstree('create_node', $(parent_node), { "text":new_node_text, "id":new_node_id, "children": [], "state": { //默認狀態展開 "opened": true } }, position, false, false); } } </script>
</body> </html> ```node