Javascript 检测文本框内容并转换为本地存储的问题
我无法将文本区域元素存储到本地存储中,当我处于静止状态并抓挠头部时,有人能给出任何指针吗Javascript 检测文本框内容并转换为本地存储的问题,javascript,local-storage,Javascript,Local Storage,我无法将文本区域元素存储到本地存储中,当我处于静止状态并抓挠头部时,有人能给出任何指针吗 // Create an event listener to save the entry when it changes // (i.e. when the user types into the textarea) function saveEntry() { // TODO: Q1(c)(iii) Task 1 of 2 // Save the text entry: //
// Create an event listener to save the entry when it changes
// (i.e. when the user types into the textarea)
function saveEntry() {
// TODO: Q1(c)(iii) Task 1 of 2
// Save the text entry:
// ...get the textarea element's current value
// (getting HTML input values is in Block 2 Part 2 Section 6)
document.getElementById("addTextEntry").value.addEventListener("click", localStorage);
// ...make a text item using the value
// (demonstrated elsewhere in this file)
// ...store the item in local storage using the given key
// (local storage is in Block 3 Part 5)
localStorage.setItem(key, JSON.item);
}
我的艾瑞文日记不编码123
我的艾瑞文日记不编码123
添加条目
添加照片
localStorage.removeItem(“”)
这是HTML如果有人需要更多信息,请告诉我,我已经尝试了一些人们发送的代码,但到目前为止还没有任何效果,请提前通知您,您的事件侦听器有点问题。这是如何将事件侦听器添加到文本区域的方法(请注意
input
event listener):
document.getElementsByClassName(“添加文本条目”)[0]。addEventListener(“输入”,函数(事件){
console.clear();
日志(event.target.value);
})
您正在设置单击事件调用本地存储
,但是没有本地存储
功能
在看不到HTML的情况下,我认为您试图做的是这样的:
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>My Erehwon Diary NOTCODE123</title>
<meta name="author" content="Stephen Rice" />
<!-- Set viewport to ensure this page scales correctly on mobile devices -
->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="tma03.css" />
<!-- Set demo data -->
<script src="tma03-demo.js"></script>
<!-- Start TMA03 application -->
<script src="tma03.js"></script>
</head>
<body>
<h1>My Erehwon Diary NOTCODE123</h1>
<main>
<section id="text" class="button">
<button type="button">Add entry</button>
</section>
<section id="image" class="button">
<button type="button">Add photo</button>
<input type="file" accept="image/*" />
</section>
</main>
</body>
<script type="text/javascript">
localStorage.removeItem("")
</script>
</html>
确保正确获取文本框的值。它应该已经是一个字符串,因此不需要使用JSON方法进一步处理它:
document.getElementById("addTextEntry").addEventListener("click", saveEntry);
function saveEntry(e) {
const addTextEntryEl = e.currentTarget.value;
// You will need to add code here to do what you want and then call your local storage below with the right parameters you're setting up here
localStorage.setItem(key, JSON.item);
}
.addEventListener
接受事件类型和侦听器。侦听器是一个函数<代码>本地存储
不是一个函数<代码>JSON。项也无效
const value = document.getElementById("addTextEntry").value;
localStorage.setItem(key, value);
您可以阅读更多关于和的信息。这看起来不像是一项真诚的努力。您是否自己尝试过编写解决方案(除了上面的假代码,它只是描述问题而不是试图解决它)?这是htmlI继续获取文档没有定义。有什么想法吗?你要把脚本插入哪里?它应该在身体的末端,但在身体内部。
document.getElementById("addTextEntry").addEventListener("click", function() {
var item = document.getElementById("addTextEntry").value;
localStorage.setItem(key, item);
});