假设您的按钮下方有一个容器,您可以将您的东西放入其中:
<button [...] />
<div id="myContainer"></div>
然后你可以通过调用填充容器
var myContainer = document.getElementById("myContainer");
var paragraph = document.createElement("p");
paragraph.appendChild(document.createTextNode("YourNumberOrTextHere"));
myContainer.appendChild(paragraph);
你会发现剩下的!;)
编辑:根据您的评论,添加以下内容:
要删除一个元素,您需要一个对它的引用。
按 ID:
//Create element and give it a unique ID
var paragraph = document.createElement("p");
paragraph.id = "element1";
//Now you can get it like this
var paragraph = document.getElementById("element1");
按标签名:
//Request all elements with a certain tag name
var paragraphs = document.getElementsByTagName("p");
//or only those of our container:
var paragraphs = document.getElementById("myContainer").getElementsByTagName("p");
//Now you can mess with all of them like this:
var paragraph = paragraphs[0]
引用:
//Just reuse the variable you used when you created your element
var paragraph = document.createElement("p");
//use paragraph
更多信息:
;)
现在:删除它:
您必须告诉 parentContainer 它应该删除一个子元素。幸运的是,每个元素都保存了对其父元素的引用:
paragraph.parentElement.removeChild(paragraph);