代码:
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
function $(id){
return document.getElementById(id);
}
function setItem(){
let key = $('key').value;
let value = $('value').value;
localStorage.setItem(key,value);
refreshDiv();
}
function refreshDiv(){
let res = ['localStorage.length: ' + localStorage.length];
for(let i = localStorage.length;i--;){
var key = localStorage.key(i);
res.push(`${key}:${localStorage.getItem(key)}`);
}
$('showDiv').innerHTML = res.join('<br/>');
}
</script>
</head>
<body>
<label>Key:<input id="key" type="text" />
</label>
<label>Value:<input id="value" type="text" />
</label><br />
<button onclick="setItem()"> SetItem </button>
<button>getItem</button>
<button>removeItem</button>
<button>clear</button>
<button>key(0)</button>
<button>length</button>
<button>remainSpace</button>
<div id="showDiv"></div>
</body>
代码前补上<!DOCTYPE html
还有><html
还有>
按顺序写这些代码不让显示出来