我正在使用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