我的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