2

我首先使用此脚本在本地存储 html 代码:

queuehistory = $("#queue").html();
localStorage.setItem("queuehistory", queuehistory); 

当页面加载时,我运行一个脚本来检测是否设置了 localstorage 项:

if (localStorage.getItem("queuehistory"))
{
  $("#queue").html(localStorage.getItem("queuehistory"));
} 

虽然 localstorage 已经设置并且确实存在,但由于某种原因,html 代码没有被加载到 $("#queue") 中。

出于测试目的,我运行了这段代码:

 if (localStorage.getItem("queuehistory"))
 {
  alert(localStorage.getItem("queuehistory"));  
  $("#queue").html(localStorage.getItem("queuehistory"));
 }

localStorage.getItem 绝对不是空的,所以我不知道为什么这段代码似乎不起作用。存储本身可以工作,但将我本地存储的 html 代码加载到 div 中似乎不起作用。任何帮助将不胜感激。

4

2 回答 2

3

将您的代码放在侧 dom 就绪回调中

$(function(){
    if (localStorage.getItem("queuehistory")){
        alert(localStorage.getItem("queuehistory"));
        $("#queue").html(localStorage.getItem("queuehistory"));
    }
})
于 2013-08-05T02:52:24.110 回答
1

localStorage 只存储字符串,不能存储一个 Dom 元素。(它可能适用于某些浏览器,但它不在规范中,所以不要以这种方式使用它)

于 2013-08-05T03:09:56.923 回答