在追加firstChild后添加按钮

时间:2014-03-18 00:02:14

标签: javascript html

基本上我想添加两个按钮来编辑和删除附加的firstChild值。

我该怎么做?

function writeParagraph() {
    var comment = prompt("Type content for new paragraph here", "");
    var newParagraph = document.createElement('p');
    newParagraph.textContent = comment;
    document.getElementById("updateDiv").appendChild(newParagraph);
}
function deleteParagraph() {
var items = document.querySelectorAll("#updateDiv p");
if (items.length) {
var child = items[0];
child.parentNode.removeChild(child);
}
}

HTML:

<div align="center"> <button onclick="store();prompter();">Click Me and type!</button>    </div>
<div align="center"> <button onclick="undo()">Undo</button></div>
<div align="center"> <button onclick="prompter2()">Set Allign</button></div>
<div align="center"> <button id="a" onclick="writepara()">Click for a new     paragraph</button></div>
<div align="center"> <button id="b" onclick="deleteParagraph()">Click to delete the new paragraph</button></div>
<div id='updateDiv' align="center"> </div>

1 个答案:

答案 0 :(得分:0)

删除将是:

function deleteParagraph() {
     var items = document.querySelectorAll("#updateDiv p");
     if (items.length) {
         var child = items[0];
         child.parentNode.removeChild(child);
     }
}

编辑将是:

function changeParagraph(newHTML) {
     var items = document.querySelectorAll("#updateDiv p");
     if (items.length) {
         var child = items[0];
         child.innerHTML = newHTML;
     }
}

您必须单独提示您要传递给changeParagraph()的任何文字。