【发布时间】:2021-12-30 18:04:13
【问题描述】:
为什么 (ndev) 变量没有添加到添加的字段类中? 当用户单击按钮时,我试图将 ndev var 附加到 addedfield 类......这里有什么问题? js代码
//1>>> first step is to creat div with the value which the user add
//get the value
const inputvalue=document.querySelector("input").value;
//create element
const div =document.createElement("div");
//adding the value and the button to it
const ndev=div.innerHTML=`${inputvalue}<input type="submit" value="delete" >`;
//2>>> style this div
div.classList.add("styleddiv");
const nst =document.querySelector(".styleddiv")
//add event listener
document.getElementById("button").addEventListener("click",
function() {
document.getElementsByClassName("addedfield").appendChild(ndev);
}
);
HTML 代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="styleSheet" href="../cssjsfirst practice.css">
</head>
<body>
<div class="container">
<form action="">
<input type="text" placeholder="type a task" autofocus>
<input type="submit" value="Add Task" id="button">
</form>
<div class="addedfield">
</div>
</div>
<script src="first js practice.js"></script>
</body>
</html>
【问题讨论】:
标签: javascript html dom events