我正在使用本地存储。我的代码在 Chrome 中完美运行,但在 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">
太迷茫