1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> #container div{ min-width: 50px; min-height: 20px; display: inline-block; padding: 5px; border: 1px solid #aaa; background: #FFF; } </style> </head> <body> <div id="container"> <div>老头 <div>蛤 <div>著作 <div>三篇</div> <div>华莱士</div> <div>谈笑风生</div> </div> <div> +1s </div> </div> <div>川 <div> 没毛病 </div> <div>刚 <div>推特</div> <div>纽约时报</div> </div> </div> </div> </div> <br> <div> <button id="DLR">前序遍历</button> <button id="LRD">后序遍历</button> </div> <br> <div> <input id="lookup-input" type="text" value="蛤"> <button id="lookup-btn">Search</button> </div> <script> var time1 = 500; var time2 = 1000; var processing = false; var container = document.getElementById('container'); var btnDLR = document.getElementById('DLR'); var btnLRD = document.getElementById('LRD'); var lookupInput = document.getElementById('lookup-input'); var lookupBtn =document.getElementById('lookup-btn');
function colorDiv(x){ x.style.background = "#aaa"; }
function uncolorDiv(x){ x.style.background = "#FFF"; }
function colorDivRed(x){ time1 += 500; time2 += 500; timer = setTimeout(function(){ x.style.background = "#ccaaaa"; },time1); } function colorThenUncolor(x){ time1 += 500; time2 += 500; timer = setTimeout(function(){ colorDiv(x); },time1); timer = setTimeout(function(){ uncolorDiv(x); },time2); }
function preOrder(x){ if(!(x==null)){ colorThenUncolor(x); for(var i=0;i<x.children.length;i++){ preOrder(x.children[i]); }
} }
function postOrder(x){ if(!(x==null)){ for(var i=0;i<x.children.length;i++){ preOrder(x.children[i]); } colorThenUncolor(x); } }
var find = 0; function lookUp(x,word){ if(!(x==null)){ if(x.firstChild.textContent.trim()==word){ colorDivRed(x); find++; }else{ colorThenUncolor(x); } for(var i=0;i<x.children.length;i++){ lookUp(x.children[i],word); }
} return find; }
btnDLR.addEventListener('click',function(){ time1 = 500; time2 = 1000; preOrder(container.firstElementChild); }); btnLRD.addEventListener('click',function(){ time1 = 500; time2 = 1000; postOrder(container.firstElementChild); });
lookupBtn.addEventListener('click',function(){ find = 0; time1 = 500; time2 = 1000; var findresult = lookUp(container.firstElementChild,lookupInput.value.trim()) timer = setTimeout(function(){ if(!findresult){ alert("Not Found!"); } },time2); }); </script> </body> </html>
|