首页 > 代码库 > H5的storage(sessionstorage&localStorage)简单存储删除

H5的storage(sessionstorage&localStorage)简单存储删除

众所周知,H5的storage有sessionstorage&localStorage,其中他们的共同特点是API相同

下面直接上代码,storage中的存储与删除:

 

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>H5的storage</title>
</head>
<body>
    <input type=‘button‘ onclick=‘setItems()‘ value=http://www.mamicode.com/‘存值‘ />"js/jquery.min.js"></script>
<script>
    //localStorage存值永久有效
    function setItems(){
        var user = {};
        user.name = ‘Adam Li‘;
        user.age  = 25;
        user.home = ‘China‘;
        localStorage.setItem(‘userinfo‘,JSON.stringify(user));
    }
    //localStorage取值
    function getItems(){
        var data = http://www.mamicode.com/JSON.parse(localStorage.getItem(‘userinfo‘));"name:"+data.name+‘\r age:‘+data.age+"\r home:"+data.home);
    }

    //localStorage删除指定键对应的值
    function deleteItem(){
        localStorage.removeItem(‘userinfo‘);
        console.log(localStorage.getItem(‘userinfo‘));
    }
</script>
</html>

H5的storage(sessionstorage&localStorage)简单存储删除