Web 存储(妙味)

2016-07-03  本文已影响0人  oigo
<body>
    <input type="checkbox" name="fruit" value="苹果">苹果<br/>
    <input type="checkbox" name="fruit" value="梨子">梨子<br/>
    <input type="checkbox" name="fruit" value="香蕉">香蕉<br/>
    <input type="checkbox" name="fruit" value="桃子">桃子<br/>
    <script>
        var inputs=document.getElementsByTagName("input");
        var len=inputs.length;
        for(var i=0;i<len;i++){
            inputs[i].onclick=function(e){
                if(this.checked){
                    window.localStorage.setItem("sel",this.value);
                }else{
                    window.localStorage.setItem("nosel",this.value);
                }
            }
        }
        window.addEventListener("storage",function(e){
            console.log(1111);
            if(e.key==="sel"){
                for(var i=0;i<len;i++){
                    if(inputs[i].value===e.newValue){
                      inputs[i].checked=true;
                    }
                }
            }else if(e.key==="nosel"){
                for(var i=0;i<len;i++){
                    if(inputs[i].value===e.newValue){
                      inputs[i].checked=false;
                    }
                }
            }
        })
    </script>
</body>

上一篇 下一篇

猜你喜欢

热点阅读