我尝试将多个可信内容条目保存到我的本地存储区以获取Chrome扩展程序。我当前的代码只保存一个满足要求的部分,但是当我尝试添加另一个单独的可信部分的ID时,它会删除所有已保存的信息,或者根本不执行任何操作。我是JS的新手,所以我希望我只是犯了一个简单的错误。 我的HTML看起来像这样:
<div id = "content">
<div id= "tcontent" contenteditable="true" data-ph=" Make a note . . . "
style= "height: 300px; overflow: auto"></div>
<div id = "content2">
<div id= "tcontent2" contenteditable="true" data-ph= " Make a note . . . "
style= "height: 300px; overflow: auto"></div>
</div>
这是我的Javascript:
window.addEventListener('load', onLoad); function onLoad() {
checkEdits();
}
function checkEdits() {
if(localStorage.userEdits!=null) {
document.getElementById("tcontent").innerHTML += localStorage.userEdits;
}
};
document.onkeyup = function (e) {
e = e || window.event;
console.log(e.keyCode);
saveEdits();
};
function saveEdits() {
var editElem = document.getElementById("tcontent");
var userVersion = editElem.innerHTML;
localStorage.userEdits = userVersion;
};
我已经尝试过向JS添加(&#34; tcontent2&#34;)但没有用。还有其中的任何变化,例如只用tcontent2重复变量。
window.addEventListener('load', onLoad);
function onLoad() {
checkEdits();
}
function checkEdits() {
if(localStorage.userEdits!=null) {
document.getElementById("tcontent","tcontent2").innerHTML += localStorage.userEdits;
}
};
document.onkeyup = function (e) {
e = e || window.event;
console.log(e.keyCode);
saveEdits();
};
function saveEdits() {
var editElem = document.getElementById("tcontent","tcontent2");
var userVersion = editElem.innerHTML;
localStorage.userEdits = userVersion;
};
感谢您的帮助:)