我一直试图在我的网页上使用localStorage和textarea(无济于事)。
我的保存脚本如下:
$(function() {
var editor = document.querySelector("#editor");
editor.addEventListener("keyup", function() {
window.localStorage["TextEditorData"] = editor.value;
});
});
我的加载脚本如下:
$window.load(function () {
var editor = document.querySelector("#editor");
if (window.localStorage["TextEditorData"]) {
editor.value = window.localStorage["TextEditorData"];
}
});
我的HTML5代码如下:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/data-load.js"></script>
<script type="text/javascript" src="js/data-save.js"></script>
</head>
<body onLoad="editor.focus()">
<textarea id="editor"></textarea>
</body>
</html>
但它似乎不起作用。我错过了一些明显的东西吗?当我使用contenteditable div而不是textarea并使用editor.innerHTML而不是editor.value时,我确实设法让它工作,但我需要为这个特定的网页使用textarea。有什么建议吗?
答案 0 :(得分:3)
您在if()
上遗漏了一些括号:
if window.localStorage["TextEditorData"] {
应该是:
if (window.localStorage["TextEditorData"]) {
目前它正在抛出语法错误。 Here's a version of your code with the above fix,工作:)