javascript - “李”元素显示为“未定义”
问题描述
开始自己做todo list来练习JS
当我将文本/任务添加到待办事项时,HTML 文本显示 -
不明确的
确保 getElementbyId 等是正确的,但不确定是否遗漏了什么。设计很垃圾,就是想试试JS。
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link type="text/css" rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<title>To doist</title>
</head>
<body>
<div class= "main-title-bar">
<h1>To doist</h1>
</div>
<div id = "button">
<button>+</button>
</div>
<div class = "input" id = "input">
<input type="text" class="add_tast" placeholder="Task">
<span onclick="newElement()" onkeypress="newElement()" class="addBtn">Add</span>
</div>
<div class = "todo-list">
<ul id="myUL">
</ul>
</div>
<script src="js/app.js"></script>
</body>
</html>
JAVASCRIPT
//Click button to get input field
let button = document.getElementById('button')
button.addEventListener("click",showInput)
function showInput(event){
if(event.type === "click"){
input.classList.toggle("show")
}
}
const input = document.querySelector(".input")
// Create new element for the classList
function newElement(){
let li = document.createElement("li");
let inputValue = document.getElementById("input").value;
let t = document.createTextNode(inputValue);
li.appendChild(t);
if (inputValue === ''){
alert("You must write something!");
} else {
document.getElementById("myUL").appendChild(li);
}
document.getElementById("input").value = "";
console.log('Is it working?');
}
期望在 HTML 上看到我的任务名称,例如“去健身房”
解决方案
你已经给id="input"
了 div 而不是 input 元素。将 id 提供给输入,它将起作用
let button = document.getElementById('button')
button.addEventListener("click",showInput)
function showInput(event){
if(event.type === "click"){
input.classList.toggle("show")
}
}
const input = document.querySelector(".input")
function newElement(){
let li = document.createElement("li");
let inputValue = document.getElementById("input").value;
let t = document.createTextNode(inputValue);
li.appendChild(t);
if (inputValue === ''){
alert("You must write something!");
} else {
document.getElementById("myUL").appendChild(li);
}
document.getElementById("input").value = "";
console.log('Is it working?');
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link type="text/css" rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<title>To doist</title>
</head>
<body>
<div class= "main-title-bar">
<h1>To doist</h1>
</div>
<div id = "button">
<button>+</button>
</div>
<div class = "input" >
<input type="text" id = "input" class="add_tast" placeholder="Task">
<span onclick="newElement()" onkeypress="newElement()" class="addBtn">Add</span>
</div>
<div class = "todo-list">
<ul id="myUL">
</ul>
</div>
<script src="js/app.js"></script>
</body>
</html>
推荐阅读
- python - 目标是找到大型算术系列开始的索引。程序什么都不给我:(
- flutter - 墨水瓶在堆栈内不起作用->定位
- c# - 限制 C# 程序使用的最大内存
- python - 使用“显示更多”按钮从网站上抓取价格信息。(无法获取所有产品的 html)
- java - 容易测试但错误:“,”或“。” 计算值和预期值的差异
- typescript - 如何在汇总中包含外部接口?
- java - quarkus 应用程序中的 Noclassdeffound 错误
- javascript - Javascript:如何在某些索引位置获取数组的值
- r - 将 Sparkline 添加到 R 中的表中
- sql - 明智地找到每个员工部门的薪水