其他分享
首页 > 其他分享> > webStorage——浏览器的本地化存储

webStorage——浏览器的本地化存储

作者:互联网

webStorage

<body>
    <h2>localStorage</h2>
    <button onclick="saveData()">保存一个数据</button>
    <button onclick="saveData()">读取一个数据</button>
    <button onclick="saveData()">删除一个数据</button>
    <button onclick="saveData()">清空数据</button>

    <script>
        let p ={name:'张三',age:18}
        
        function saveData(){
            localStorage.setItem('msg','hello!!!')
            localStorage.setItem('msg',666)
            localStorage.setItem('msg',p)
        }
        function readData(){
            console.log(localStorage.getItem('msg'))
            console.log(localStorage.getItem('msg2'))

            const result =  localStorage.getItem('person')
            console.log(JSON.parse(result))

            console.log(localStorage.getItem('msg3'))
        }
        function deleteData(){
            localStorage.removeItem('msg2')
        }
        function deleteAllData(){
            localStorage.clear()
        }
    </script>
</body>

标签:存储,浏览器,键名,本地化,localStorage,getItem,console,webStorage,log
来源: https://www.cnblogs.com/wzx-blog/p/15977595.html