简体   繁体   English

如何制作一个在单击时创建按钮的按钮(在 HTML 和 JavaScript 中)?

[英]How to make a button that creates a button when clicked (in HTML and JavaScript)?

Basically the question.基本上是这个问题。 My code (below) is quite lengthy and probably terrible as I am very new to coding.我的代码(如下)很长而且可能很糟糕,因为我对编码很陌生。 I am aware that I should put the JavaScript and HTML in different documents, but I prefer it like such.我知道我应该将 JavaScript 和 HTML 放在不同的文档中,但我更喜欢这样。 What I'd like to do is create a table, that, when users enter a column, a button appears next to the column giving them the option to delete it had they made a mistake.我想做的是创建一个表格,当用户输入一列时,该列旁边会出现一个按钮,让他们可以选择删除它,如果他们犯了错误。

 document.getElementById("add").onclick = function() { var node = document.createElement("Tr"); document.getElementById("list").appendChild(node); var node = document.createElement("Td"); var text = document.getElementById("w").value; var textnode = document.createTextNode(text); node.appendChild(textnode); document.getElementById("list").appendChild(node); var node = document.createElement("Td"); var text = document.getElementById("m").value; var textnode = document.createTextNode(text); node.appendChild(textnode); document.getElementById("list").appendChild(node); var node = document.createElement("Td"); var text = document.getElementById("p").value; var textnode = document.createTextNode(text); node.appendChild(textnode); document.getElementById("list").appendChild(node); }
 <table id="list"> <tr> <th scope="col">Word</th> <th scope="col">English</th> <th scope="col">Pronunciation</th> </tr> <tfoot> <tr> <div class="boxy_bois"> <td><input type="text" id="w" class="w" required/></td> <td><input type="text" id="m" class="m" required/></td> <td><input type="text" id="p" class="p" /></td> </div> </tr> </tfoot> </table> <div class="button_boi"> <button class="button_boi" id="add">Enter</button> </div>

using JQuery Library in your Javascript would save you all the hustle with very simple and short codes在你的 Javascript 中使用 JQuery 库可以用非常简单和简短的代码为你省去所有的麻烦

$("#add").on("click", function(){
    $("#list").html("your button element here");
});

to remove it删除它

$("#yourbuttonID").on("click", function(){
    $(this).remove();
});

Your code create incorrect HTML result.您的代码创建了不正确的 HTML 结果。 It becomes <tr></tr><td></td>... instead of <tr><td></td>...</tr> .它变成<tr></tr><td></td>...而不是<tr><td></td>...</tr>

With current JavaScript, you can create new element using string and insert to exists HTML using insertAdjacentHTML() .使用当前的 JavaScript,您可以使用字符串创建新元素并使用insertAdjacentHTML()插入现有的 HTML。 It is a lot easier than create DOM object and appendChild() .它比创建 DOM 对象和appendChild()容易得多。

 document.getElementById("add").onclick = function() { let listTable = document.getElementById("list"); let tfoot = listTable.querySelector('tfoot'); let wordValue = document.getElementById("w").value; let engValue = document.getElementById("m").value; let pronunValue = document.getElementById("p").value; let trHTML = '<tr>' + '<td>' + wordValue + '</td>' + '<td>' + engValue + '</td>' + '<td>' + pronunValue + '<button class="deletethis" type="button" title="delete this row">x</button></td>' + '</tr>'; tfoot.insertAdjacentHTML('beforebegin', trHTML); } // use event delegation to listen on click delete row. document.addEventListener('click', (e) => { let thisTarget = e.target; let thisBtn = thisTarget.closest('.deletethis'); if (thisBtn) { // if correct button then delete the whole table row (tr). e.preventDefault(); thisTarget.closest('tr').remove(); } });
 <table id="list"> <tr> <th scope="col">Word</th> <th scope="col">English</th> <th scope="col">Pronunciation</th> </tr> <tfoot> <tr> <div class="boxy_bois"> <td><input type="text" id="w" class="w" required /></td> <td><input type="text" id="m" class="m" required /></td> <td><input type="text" id="p" class="p" /></td> </div> </tr> </tfoot> </table> <div class="button_boi"> <button class="button_boi" id="add">Enter</button> </div>

From the code above, I use the new querySelector() , closest() to select the near elements.从上面的代码中,我使用新的querySelector()closest()来选择邻近的元素。

I use event delegation to detect delete row button were click and then delete the whole tr because newly created element can't detect when using normal addEventListener .我使用事件委托来检测删除行按钮被单击然后删除整个tr因为新创建的元素在使用普通addEventListener时无法检测到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM