HTML5JavaScript。如何保存文本区域输入,然后通过按钮加载
我有一个项目正在进行中,其中有一个“保存”按钮,用于将用户的数据保存到HTML5JavaScript。如何保存文本区域输入,然后通过按钮加载,javascript,html,Javascript,Html,我有一个项目正在进行中,其中有一个“保存”按钮,用于将用户的数据保存到localStorage,还有一个“加载”按钮,用于从localStorage加载用户保存的数据。但是,它不起作用。有人能帮我解决这个问题吗 HTML: 您必须将函数分配给单击onclickproperties。您正在调用doSave和doLoad并分配它们的返回值。由于这些函数没有return语句,因此它们返回undefined 卸下()。不要立即给他们打电话。您不正确地使用了localStorage: function d
localStorage
,还有一个“加载”按钮,用于从localStorage
加载用户保存的数据。但是,它不起作用。有人能帮我解决这个问题吗
HTML:
您必须将函数分配给单击onclickproperties。您正在调用
doSave
和doLoad
并分配它们的返回值。由于这些函数没有return
语句,因此它们返回undefined
卸下
()
。不要立即给他们打电话。您不正确地使用了localStorage
:
function doSave(){
//Set the item in doSave()
//localStorage.setItem("text", text.value);
}
function doLoad(){
//Get the item in doLoad()
//text.value = localStorage.getItem("text");
}
另外,请阅读的答案:设置onclick
事件时,不要调用doSave()
和doLoad()
:
//When the window loads...
window.onload = function(){
saveButton = document.getElementById("save");
saveButton.onclick = doSave;
loadButton = document.getElementById("load");
loadButton.onclick = doLoad;
textarea = document.getElementById("text");
};
这是“小提琴”:但它不是吗?你的问题是什么?@putvande使用上下文线索。这是通过一个保存按钮将他输入的内容保存到
元素中。@NobleMushtak-虽然可以从提供的信息中找出问题所在,但OP仍然应该进行一些基本的调试并报告结果,而不是仅仅给出一个“它不起作用”的帖子,您不能使用text.value
访问textarea的内容,但是textarea.value
@JbDrucker-您可能可以通过IE 4中开始并在浏览器生态系统的其余部分传播的可怕的“id为的DOM元素获取全局变量”问题来访问textarea的内容。@JbDrucker:id为的元素现在是可靠的全局变量,所以这很好用。或者,好吧,这是个新概念,但我会尝试修复它。汉克斯会努力的,只是html5和javascript的新概念
function doSave(){
//Set the item in doSave()
//localStorage.setItem("text", text.value);
}
function doLoad(){
//Get the item in doLoad()
//text.value = localStorage.getItem("text");
}
//When the window loads...
window.onload = function(){
saveButton = document.getElementById("save");
saveButton.onclick = doSave;
loadButton = document.getElementById("load");
loadButton.onclick = doLoad;
textarea = document.getElementById("text");
};