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

コードを side dom ready コールバックに配置します

$(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 に答える