首页 > 代码库 > onhashchange

onhashchange

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>Document</title> 6 </head> 7 <body> 8     <input type="button" value="随机" id="btn1"> 9     <div id="div1"></div>10     <script>11     var oBtn = document.getElementById("btn1");12     var oDiv = document.getElementById("div1");13     var obj = {};14 15     oBtn.onclick = function(){16         var number = randomNum(35,7);17         oDiv.innerHTML = number;18 19         var oRD = Math.random();20         obj[oRD] = number;21 22         window.location.hash = oRD;23     };24 25     window.onhashchange = function(){26         //undefined时为空27         var number = obj[ window.location.hash.substring(1) ] || "";//去掉#28         oDiv.innerHTML = number;29     };30     function randomNum(alls,now){31         var arr = [];32         var newArr = [];33 34         for(var i = 1;i <= alls;i ++){35             arr.push(i);36         }37         38         for(var i = 0;i < now;i ++){39             //Math.random()   [0,1)40             //Math.floor( Math.random() * arr.length   0~3441             newArr.push( arr.splice(Math.floor( Math.random() * arr.length ),1) );//删除并返回被删除元素的数组42         }43         return newArr;//是一个二维数组44     }45 46     </script>47 </body>48 </html>

 

onhashchange