h5中hash的用法实例

<!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 = {};


    oBtn.onclick = function(){
        var number = randomNum(35,7);
        oDiv.innerHTML = number;
        var MRN = Math.random();
        obj[MRN] = number;
        window.location.hash = MRN;

    }
    window.onhashchange = function(){
        var number = obj[window.location.hash.substring(1)] || '';
        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>

 

posted @ 2018-05-07 14:07  阿弥陀佛么么哒!  阅读(346)  评论(0编辑  收藏  举报