zoukankan      html  css  js  c++  java
  • localstorge实现本地存储

    这个是我个人生活中的一个小小的案例,由于我最近在找工作,投的简历多了,就很容易弄混淆,我就弄了这个个小东西。

    最开始没有实现本地存储的功能的,只是当作一个闭包的案例,具体看这里,但是这样子,一刷新,就会消失不见了,所以我就加入了一个本地存储的功能

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <form>
            <input type="text" name="" id="text">
            <input type="submit" name="" value="添加" id="button">
        </form>
        <ol id="box">
        </ol>
        <script type="text/javascript">
        // 获取页面元素
         function GetId(id) {
            return document.getElementById(id)
        }
        var button = GetId("button")
        var text = GetId("text")
    
        function getLocalStorage (){
            var hasPay_ArrayList = localStorage.getItem("isPayLocal")==""||localStorage.getItem("isPayLocal")=="null"||localStorage.getItem("isPayLocal")==null?[]:localStorage.getItem("isPayLocal").split(","),
                value = text.value
                console.log(hasPay_ArrayList)
            if(hasPay_ArrayList.length){
                for (var i = hasPay_ArrayList.length - 1; i >= 0; i--) {
                var li = document.createElement('li')
                li.innerHTML = hasPay_ArrayList[i] + '<br>'
                var box = GetId("box")
                box.appendChild(li)
                }
            }
    
        }
        getLocalStorage()
    button.addEventListener(
    'click', function(e) { e.preventDefault() // 获取浏览器本地localStorage,可能是null,可能是“” var hasPay_ArrayList = (localStorage.getItem("isPayLocal")==""||localStorage.getItem("isPayLocal")=="null"||localStorage.getItem("isPayLocal")==null)?[]:localStorage.getItem("isPayLocal").split(",") console.log(hasPay_ArrayList) console.log(localStorage.getItem("isPayLocal")) var value = text.value; var isLive = hasPay_ArrayList.indexOf(value)>=0?true:false; console.log(isLive) if (value && !isLive) { var li = document.createElement('li') li.innerHTML = value + '<br>' var box = GetId("box") box.appendChild(li) text.value = '' var refreshLocalstorage = hasPay_ArrayList.push(value) localStorage.setItem("isPayLocal",hasPay_ArrayList) console.log(hasPay_ArrayList) console.log(localStorage.setItem("isPayLocal",hasPay_ArrayList)) } else if (value&&isLive) { alert("已经投递") }else{ alert("输入不能为空") } text.value='' }) </script> </body> </html>

    当然了,既然是完善功能,那么原来的功能也就是不能发生变化的,这个检查是否已经投递的功能还在,这个新增加的存储功能,也是非常的简单的,当然还是可以自己去改变,多多的添加一些新的功能,让这个例子更加的完善,但是我目前就做了这么些的东西。出具雏形,以后再慢慢的添砖加瓦吧

  • 相关阅读:
    OpenStack报错:MessagingTimeout: Timed out waiting for a reply to message ID
    Missing value auth-url required for auth plugin password
    解决eth0网卡无法自动加载的问题
    Linux中一个网卡含有多个IP,将从IP升级为主IP的方法
    Ubuntu系统上双节点部署OpenStack
    Ubuntu系统上All-in-one部署OpenStack
    ubuntu系统中添加DNS服务器地址后诡异消失的解决办法
    解决Ubuntu14.04安装Chrome浏览器打不开的问题
    搭建HBase的本地模式、伪分布式、全分布式和HA模式
    PostgreSQL 与 MySQL 相比,优势何在?【转】
  • 原文地址:https://www.cnblogs.com/yiyistar/p/7499473.html
Copyright © 2011-2022 走看看