我正在使用localStorage。我的代碼完全適用於Chrome,但不適用於IE9和Firefox。localStorage不能在IE9和Firefox中工作
下面是代碼:
document.addEventListener("DOMContentLoaded", restoreContents, false);
document.getElementsByTagName("body")[0].onclick=function(){saveContents('myList','contentMain', event, this);};
function amIclicked(e, eleObject)
{
alert("amIClicked");
e = e || event;
var target = e.target || e.srcElement;
alert("target = " + target.id);
if(target.id=='pageBody' || target.id=='Save')
return true;
else
return false;
}
function saveContents(e, d, eveObj, eleObject) {
//alert("saveContents");
if (amIclicked(eveObj, eleObject)) {
var cacheValue = document.getElementById(e).innerHTML;
var cacheKey = "key_" + selectedKey;
var storage = window.localStorage;
//alert ("cacheKey = " + cacheKey + " ,cacheValue = " + cacheValue);
if(typeof(Storage)!=="undifined"){
localStorage.setItem("cacheKey","cacheValue");
}
//alert ("Saved!!");
var dd = document.getElementById(d);
//document.getElementById("contentMain").style.display == "none";
dd.style.display = "none";
}
}
function restoreContents(e,k) {
//alert("test");
if(k.length < 1) { return; }
var mySavedList = localStorage["key_" + k];
if (mySavedList != undefined) {
document.getElementById(e).innerHTML = mySavedList;
}
}
<a onclick="ShowContent('contentMain','myList','Sample_1'); return true;" href="#" >Sample 1</a><br/><br/>
<a onclick="ShowContent('contentMain','myList','Sample_2'); return true;" href="#" >Sample 2</a><br/><br/>
<div style="display:none;display:none;position:absolute;border-style: solid;background-color: white;padding: 5px;"id="contentMain">
<ol id="myList" contenteditable="true">
<li>Enter Content here</li>
</ol>
<!--<input id="editToggleButton" type="button" value="Edit"/>-->
</div>
,當我試圖使用Firebug調試Firefox中的代碼,我得到的錯誤在不同的線路。我完全糊塗了這裏:)
下面是代碼,我正在錯誤:
function amIclicked(e, eleObject)
{
alert("amIClicked");
e = e || event;
var target = e.target || e.srcElement;
alert("target = " + target.id);
if(target.id == 'pageBody' || target.id == 'Save'){
return true;
}
else{
return false;
}
}
而且我在Mozilla Firefox得到的錯誤是:
target is undefined
[Break On This Error]
alert("target = " + target.id);
我宣佈在
<body id="pageBody">
太多困惑
目標3210
能錯字可能會導致您的問題的人呢? 'if(typeof(Storage)!==「undifined」){'Undefined拼寫錯誤。不知道這是如何在Chrome中工作。這是你真實的,真實的代碼嗎? – jmort253
你在使用localhost嗎? localstorage只能在HTTP網站上使用。 –
@ eric.itzhak localhost通過http(我在本地主機上使用了localStorage),你可能意指文件系統 – Musa