为什么我不能动态创建html元素?

我想创建动态段落,因此我编写了以下代码。用户单击按钮后,应添加一个新的段落,该段落的innerText是用户的输入。但是我失败了,有人可以帮助我吗?


var input = document.querySelector(".input");

var btn = document.querySelector("button");

var body = document.querySelector("body");


btn.addEventListener('click', addParagraph);


function addParagraph() {

  var childELes = body.children;

  for (var p in childELes) {

    if (p.tagName === "p")

      p.remove();

  }

  if (input.value.trim() != "") {

    var newPara = document.createElement("p");

    newPara.innerText = input.value;

    body.appendChild(newPara);

  }

}

<!DOCTYPE html>

<html>


<head>

  <title>test</title>

</head>


<body>

  <form>

    <p>

      <label>input:</label>

      <input type="text" class="input">

      <button>add</button>

    </p>

  </form>

</body>


</html>


潇湘沐
浏览 235回答 3
3回答

波斯汪

您的代码正在运行,但是正在提交表单,这就是为什么动态增加的价值丢失并刷新页面的原因。为了使其按预期工作,您需要使用e.preventDefault();阻止提交表单的方法:var input = document.querySelector(".input");var btn = document.querySelector("button");var body = document.querySelector("body");btn.addEventListener('click', addParagraph);function addParagraph(e) {&nbsp; e.preventDefault();&nbsp; var childELes = body.children;&nbsp; for (var p in childELes) {&nbsp; &nbsp; if (p.tagName === "p")&nbsp; &nbsp; &nbsp; p.remove();&nbsp; }&nbsp; if (input.value.trim() != "") {&nbsp; &nbsp; var newPara = document.createElement("p");&nbsp; &nbsp; newPara.innerText = input.value;&nbsp; &nbsp; body.appendChild(newPara);&nbsp; }}<form>&nbsp; <p>&nbsp; &nbsp; <label>input:</label>&nbsp; &nbsp; <input type="text" class="input">&nbsp; &nbsp; <button>add</button>&nbsp; </p></form>

有只小跳蛙

添加type="button"到您的按钮html标签:<!DOCTYPE html><html><head>&nbsp; &nbsp; <title>test</title></head><body>&nbsp; &nbsp; <form>&nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <label>input:</label>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" class="input">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button type="button">add</button>&nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; </form>&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; var input = document.querySelector(".input");&nbsp; &nbsp; var btn = document.querySelector("button");&nbsp; &nbsp; var body = document.querySelector("body");&nbsp; &nbsp; btn.addEventListener('click', addParagraph);&nbsp; &nbsp; function addParagraph() {&nbsp; &nbsp; &nbsp; &nbsp; var childELes = body.children;&nbsp; &nbsp; &nbsp; &nbsp; for (var p in childELes) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (p.tagName === "p")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; p.remove();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (input.value.trim() != "") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var newPara = document.createElement("p");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newPara.innerText = input.value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; body.appendChild(newPara);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; </script></body></html>

慕尼黑8549860

您可以添加.preventDefault()以防止button刷新页面。var input = document.querySelector(".input");var btn = document.querySelector("button");var body = document.querySelector("body");btn.addEventListener('click', addParagraph);function addParagraph(e) {&nbsp; e.preventDefault();&nbsp; var childELes = body.children;&nbsp; for (var p in childELes) {&nbsp; &nbsp; if (p.tagName === "p")&nbsp; &nbsp; &nbsp; p.remove();&nbsp; }&nbsp; if (input.value.trim() != "") {&nbsp; &nbsp; var newPara = document.createElement("p");&nbsp; &nbsp; newPara.innerText = input.value;&nbsp; &nbsp; body.appendChild(newPara);&nbsp; }}<!DOCTYPE html><html>&nbsp; <head>&nbsp; &nbsp; <title>test</title>&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <form>&nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; <label>input:</label>&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" class="input">&nbsp; &nbsp; &nbsp; &nbsp; <button>add</button>&nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; </form>&nbsp; </body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript