将用户本地存储的值插入到加载功能中

时间:2014-06-05 13:27:10

标签: jquery html5 edit

您好我有一个问题我想将用户本地存储的值加载到一个函数中并在该函数中检查是否提供localstorage如果其可用的加载本地用户存储如果没有加载页面这是我的脚本到目前为止工作第一部分。

        function LoadSample(num){
//Here is the Problem i need sone help please ;)
        $("#code"+num).load('sites/demo'+num+'.html');   if(localStorage.userEdits!=null)
        document.getElementById("code1").html = localStorage.userEdits;
       $("#livebtn"+num).css("display", "none");
       $("#codebtn"+num).css("display", "block");
       $("#savebtn"+num).css("display", "none");
       $("#update").css("display", "none");


}

function LoadCode(num){
   $.get('sites/demo'+num+'.html', function(data) {
       $("#code"+num).text(""+data+"");
       if(localStorage.userEdits!=null)
       document.getElementById("code1").innerHTML = localStorage.userEdits;

   });
   $("#codebtn"+num).css("display", "none");
   $("#livebtn"+num).css("display", "block");
   $("#savebtn"+num).css("display", "none");
   $("#update").css("display", "none");
}



</script>
<script>
function saveEdits() {

//get the editable element
var editElem = document.getElementById("edit");

//get the edited element content
var userVersion = editElem.innerHTML;

//save the content to local storage
localStorage.userEdits = userVersion;

//write a confirmation to the user
document.getElementById("update").innerHTML="Änderungen gespeichert!";

}

function checkEdits() {

//find out if the user has previously saved edits
if(localStorage.userEdits!=null)
document.getElementById("edit").innerHTML = localStorage.userEdits;
}

</script>

1 个答案:

答案 0 :(得分:0)

检测本地存储是否可用(来自最新的Modernizr test):

...
var mod = 'modernizr';
try {
    localStorage.setItem(mod, mod);
    localStorage.removeItem(mod);
    return true;
} catch(e) {
    return false;
}
...