我正在尝试修改以下代码,以便它可以保存多个已编辑的文本,而目前此代码仅保存一个(第一个)已编辑的文本...有人可以为我修改此代码,例如:您修改它以保存 3或 4 个编辑过的文本,基于此我可以添加我需要的任意数量的文本字段。如果您在文本字段中写入,保存它然后刷新页面,您的书面内容仍然存在,就像您尝试使用以下代码和一个文本字段一样,我希望在添加多个文本时也应用此功能字段。
<html>
<head>
<title>Allowing Users to Edit Multiple Text field and save the contents</title>
<script type="text/javascript">
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="Edits saved!";
}
function checkEdits() {
//find out if the user has previously saved edits
if(localStorage.userEdits!=null)
document.getElementById("edit").innerHTML=localStorage.userEdits;
}
</script>
</head>
<body onload="checkEdits()">
<div id="edit" contenteditable="true">
Here is the element's original content
</div>
<input type="button" value="save my edits" onclick="saveEdits()"/>
<div id="update"> Edit the text and click to save for next time</div>
</body>
</html>
提前致谢。
这就是我尝试过的。
<html>
<head>
<title>Allowing Users to Edit Multiple Text field and save the contents</title>
<script type="text/javascript">
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;
// for new text field
localStorage.userEdit1 = userVersion;
//write a confirmation to the user
document.getElementById("update").innerHTML="Edits saved!";
}
function checkEdits() {
//find out if the user has previously saved edits
if(localStorage.userEdits!=null)
document.getElementById("edit").innerHTML=localStorage.userEdits;
}
</script>
</head>
<body onload="checkEdits()">
<div id="edit" contenteditable="true">
Here is the element's original content
</div>
<!--New text field -->
<div id="edit" contenteditable="true">
This is another text field.
</div>
<input type="button" value="save my edits" onclick="saveEdits()"/>
<div id="update"> Edit the text and click to save for next time</div>
</body>
</html>