关于这个 javascript 代码有什么问题有什么想法吗?
我有下面的代码。该代码的目的是获取本地存储中存储的所有值,并将它们显示在两个 id
为 'title'
和 'textLoc 的 HTML 元素中'
。 'title'
是 ,
'textLoc'
是
var tests = [];
var titles = [];
var finalTests = "";
var key, value;
for (var i = 0; i < localStorage.length; i++) {
key = localStorage.key(i);
value = localStorage.getItem(key);
tests.push(value);
titles.push(key);
finalTests += "<tr><td><a class=\"dashlinks\" href=\"javascript:void\" onclick=\"rememberTest("+i+")\">" + key + "</a></td></tr>";
}
for (i=0; i<tests.length; i++) {
document.getElementById('title').innerHTML = titles[i];
document.getElementById('textLoc').innerHTML = tests[i];
}
I have the code below. The purpose of the code is to grab all the values stored in the local storage and display them in two HTML elements with id
s of 'title'
and 'textLoc'
. 'title'
is an <input type="text">
and 'textLoc'
is a <textarea>
. I want the values to be stored in the <textarea>
and the keys to be stored in the <input type="text">
. The values are being stored correctly but the keys are not. Any ideas on why this would be?
var tests = [];
var titles = [];
var finalTests = "";
var key, value;
for (var i = 0; i < localStorage.length; i++) {
key = localStorage.key(i);
value = localStorage.getItem(key);
tests.push(value);
titles.push(key);
finalTests += "<tr><td><a class=\"dashlinks\" href=\"javascript:void\" onclick=\"rememberTest("+i+")\">" + key + "</a></td></tr>";
}
for (i=0; i<tests.length; i++) {
document.getElementById('title').innerHTML = titles[i];
document.getElementById('textLoc').innerHTML = tests[i];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用
document.getElementById('title').value
和document.getElementById('textLoc').value
。另外,在存储 FinalTests 后,您似乎没有对它进行任何操作。You should use
document.getElementById('title').value
anddocument.getElementById('textLoc').value
. Also it seems like you are doing nothing with finalTests after you store it.您应该将字符串附加到文本区域:
You should be appending the string to the text area: