如何修复我的清除按钮以删除数组

我真的需要帮助来清除按钮的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>

xiaoxianchao 回答:如何修复我的清除按钮以删除数组

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

    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>

谢谢!

,
function Clear() {
 document.getElementById("result").textContent ="";
 document.getElementById("txtName").value ="";
}
本文链接:https://www.f2er.com/3046869.html

大家都在问