H5localStorage的简单储存和删除

<!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='存值' />
<input type='button' onclick='getItems()' value='取值' />
<input type='button' onclick='deleteItem()' value='删除' />

</body>
<script src=”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 = JSON.parse(localStorage.getItem('userinfo'));
    console.log("name:"+data.name+'\r age:'+data.age+"\r home:"+data.home);
}

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

</script>
</html>

    原文作者:qzuser
    原文地址: https://segmentfault.com/a/1190000013465295
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞