<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
var storage = {
ls: window.localStorage,
setItem: function () {
return this.ls.setItem.apply(this.ls, arguments);
},
getItem: function () {
return this.ls.getItem.apply(this.ls, arguments);
},
removeItem: function () {
return this.ls.removeItem.apply(this.ls, arguments);
},
clear: function () {
return this.ls.clear.apply(this.ls, arguments);
}
};
storage.setItem("name", "小磊");
//storage.clear("name");
console.log(storage.getItem("name"));
/* 如果希望存储在本地,可以直接调用JSON.stringify()将其转为字符串。读取出来后调用JSON.parse()将字符串转为json格式,如下所示:
var details = {author:"isaac","description":"fresheggs","rating":100};
storage.setItem("details",JSON.stringify(details));
details = JSON.parse(storage.getItem("details"));*/
</script>
</body>
</html>