h5中history实例
<!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> <button id="btn">按钮</button> <div id="div1"> </div> </body> <script> let oBtn = document.getElementById('btn'); let oDiv = document.getElementById('div1'); let obj = {}; let iNow = 1; oBtn.onclick = function(){ var number = randomNum(35,7); oDiv.innerHTML = number; window.history.pushState(null,'',iNow++); //注意必须在服务器环境下,否则会报错 } window.onpopstate = function(ev){ var number = ev.state || ''; oDiv.innerHTML = number; } function randomNum(alls,now){ var arr = []; var newArr = []; for(var i=1;i<=alls;i++){ arr.push(i); } for(var i=0;i<now;i++){ newArr.push(arr.splice(Math.floor(Math.random()*arr.length),1)); } return newArr; } </script> </html>