我的localStorage返回null,我不知道为什么,read函数没有被使用,但只是为了帮助,我反正把它放了。 Chrome表示无法将innerHTML置入null,并且我的疑难解答警报信息也会返回null,但代码将结束。任何帮助将是有用的,谢谢。
该Cookie:HTML localStorage返回NULL
<!doctype html>
<html>
<head>
<cookie>
<script>localStorage.setItem('id',Math.floor(Math.random()*10))
</script>
</cookie>
</head>
<body>
<script>var id = localStorage.getItem('id')
alert(id)</script>
</body>
</html>
脚本:
<!doctype html>
<html>
<head>
<script>
var theId=localStorage.getItem('id')
function change(id,target){
var info = localStorage.getItem(id);
alert(info)
document.getElementById(target).innerHTML=info;
}
function read(){
var element=document.createElement('h1')
element.innerHTML='You Want To Read'
document.body.appendChild(element)
alert('debug')
}
</script>
</head>
<body>
<input type="button" value="Read" name="read_story" id="read_story"
onclick=read();change(theId,info)>
<p id='info'>initial</p>
</body>
<script>
alert('debug');
</script>
</html>
你试过只是设置一个硬编码的值,并看到后,如果getItem的作品? – ThisGuyHasTwoThumbs