js 遍歷tree的一個例子
阿新 • • 發佈:2018-11-24
div wid content 自己 rip tar ray class head
小例子蘊含著到力量,我希望自己有改變自己生活的能力,自己有主宰自己命運的能力...
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <div> <h1 onclick="test()">test</h1> </div> <script> var arrs = []; function test(){ var data = [ { name: ‘中國‘, children: [ { name:‘教第三節課‘, }, { name: ‘教呼呼‘, children: [ { name: ‘大一‘, children: [ { name:‘課程1‘, children: [ { name: ‘1231‘ }, { name: ‘121‘ } ] }, { name: ‘課程2‘, children: [ { name: ‘1232‘ }, { name: ‘122‘ } ] }, { name: ‘課程3‘, children: [ { name: ‘1233‘ }, { name: ‘123‘ } ] }, ] } ] }, { name: ‘活動‘, children:null } ] } ]; getArray(data,‘活動‘); for(var i in undefined){ alert(123); } } function getArray(data,name) { for (var i in data) { if (data[i].name == name) { break; } else { getArray(data[i].children, name); } } } </script> </body> </html>
js 遍歷tree的一個例子