javascript - 如何修复我的清除按钮以删除阵列

标签 javascript arrays

我真的需要帮助来清除按钮的 onclick。努力理解输入后如何清除数组名称。我最近才开始学习 Javascript,我对 Html 的了解也很一般。

function Add() {
  //create a para obj
  var newPara = document.createElement("p");

  //create the text content
  var input = document.getElementById("txtName").value;
  var newText = document.createTextNode(input);

  //attach the text to the para obj
  newPara.appendChild(newText);

  //attach the para obj to the div obj
  var myDiv = document.getElementById("result");
  myDiv.appendChild(newPara);
}

function Clear() {
  var newPara = document.getElementById("tostring")
  input = ""
  newPara.value = 0;
}

function Count() {
  var arrPara = document.getElementById("result").childNodes;
  document.getElementById("result").removeChild(arrPara[Index.Count]);
}
<label>Name</label><input id="txtName" type="text" /><br />
<p id="displayNames"></p>
<button onclick="Add()">Add</button>
<!-- add the name to 
 an array and display the name on the paragraph -->
<button onclick="Clear()">Clear</button>
<!-- clear the 
names in the array and clear the paragraph -->
<button onclick="Count()">Count</button>
<!-- count and 
display the number of times the name has been entered -->
<div id="result"></div>

最佳答案

当您单击按钮时,它将清除输入值。

    function Add() {
      //create a para obj
      var newPara = document.createElement("p");

      //create the text content
      var input = document.getElementById("txtName").value;
      var newText = document.createTextNode(input);

      //attach the text to the para obj
      newPara.appendChild(newText);

      //attach the para obj to the div obj
      var myDiv = document.getElementById("result");
      myDiv.appendChild(newPara);
      
      document.getElementById('txtName').value = '';
    }

    function Clear() {
      var newPara = document.getElementById("result")
      newPara.textContent = '';
      console.log(newPara)
      newPara.value = 0;
    }

    function Count() {
      var arrPara = document.getElementById("result").childNodes;
      document.getElementById("result").removeChild(arrPara[Index.Count]);
    }
    <label>Name</label><input id="txtName" type="text" /><br />
    <p id="displayNames"></p>
    <button onclick="Add()">Add</button>
    <!-- add the name to 
     an array and display the name on the paragraph -->
    <button onclick="Clear()">Clear</button>
    <!-- clear the 
    names in the array and clear the paragraph -->
    <button onclick="Count()">Count</button>
    <!-- count and 
    display the number of times the name has been entered -->
    <div id="result"></div>

谢谢!

关于javascript - 如何修复我的清除按钮以删除阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59005092/

相关文章:

javascript - 重新加载页面或手动键入 URL,如 "localhost/mysite.com/contact"在 Angular js 中在服务器上给出错误的 "Not Found"(404 错误)

c - 将字符串作为输入到字符串指针数组中并显示它们

通过引用的 JavaScript 数组/函数对象

java - Java 中整数数组的 Arraylist 的排序和比较

带有填充数组的 C 简单代码

javascript - 正在抓取 : session ID from browser works,,但抓取的 session ID 却没有

javascript - 一次显示一个隐藏的 div

c++ - 如何制作需要执行此操作的功能?

javascript - 推特分享按钮怪癖

javascript - 在我的情况下,非盲目点击无法删除点击事件