简体   繁体   English

我不能 append 在 js 文件中创建的 div<section> ,在我的 index.html 中的网格内</section>

[英]I'm can't append a div created in js file inside a <section>, whitch is inside a grid, in my index.html

I'm trying to create a to-do list and use html/css/js to do it.我正在尝试创建一个待办事项列表并使用 html/css/js 来完成它。

  • I've created a button, and a text input on HTML.我在 HTML 上创建了一个按钮和一个文本输入。
  • Created a function in JS.在 JS 中创建了一个 function。
  • Got both by their id.通过他们的ID得到了两个。
  • Created a div in JS.在 JS 中创建了一个 div。
  • inserted the the text that I got from text input inside the var that holds the div.将我从文本输入中获得的文本插入到包含 div 的 var 中。
  • and tryied to append this div into the section using appenddChild.并尝试将 append 这个 div 放入使用 appenddChild 的部分。

When I click the button I can see the div apearing for less than a second inside the section, and it desapears.当我单击按钮时,我可以看到 div 在该部分内显示不到一秒钟,然后消失。

 function addTask() { let userInput = document.getElementById("add-field").value; let task = document.createElement("div"); task.innerText = userInput; taskList = document.getElementById("list"); task.className = "task-bar"; document.querySelector('#list').appendChild(task); }
 <main id="container"> <section id="entrance"> <h1>TASKS</h1> </section> <section id="adding"> <form id="adding-items"> <input id="add-field" type="text"> <button id="send-btn" value="ADD" onclick="addTask()">ADD</button> </form> </section> <section id="list"> </section> </main>

在此处输入图像描述

You are using a form element.您正在使用form元素。 So if you click the button, it would refresh the page.因此,如果您单击按钮,它将刷新页面。

To prevent it, you can use onSubmit="return false;"为了防止它,您可以使用onSubmit="return false;" . .

<form onSubmit="return false;" id="adding-items">

Its because you're using a <form> element.这是因为您使用的是<form>元素。 Forms have a default behavior that when it contains a button, that button will be used to submit the form, unless the type of the button is other than submit . Forms 有一个默认行为,当它包含一个按钮时,该按钮将用于提交表单,除非按钮的type不是submit

You'll need to stop the default behavior of the form from happening.您需要阻止表单的默认行为发生。 You do this by preventing the default behavior of the submit event.您可以通过阻止提交事件的默认行为来做到这一点。

You do this with Event.preventDefault() inside the event handler.您可以在事件处理程序中使用Event.preventDefault()来执行此操作。

 const form = document.querySelector('#adding-items'); const taskList = document.querySelector('#list'); const userInput = document.getElementById("add-field") function addTask() { let userInputValue = userInput.value; let task = document.createElement("div"); task.innerText = userInputValue; task.className = "task-bar"; taskList.appendChild(task); } form.addEventListener('submit', event => { event.preventDefault(); // Stop the default behavior. addTask(); });
 <main id="container"> <section id="entrance"> <h1>TASKS</h1> </section> <section id="adding"> <form id="adding-items"> <input id="add-field" type="text"> <button id="send-btn" value="ADD">ADD</button> </form> </section> <section id="list"> </section> </main>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM