【发布时间】:2020-11-29 18:13:21
【问题描述】:
如何添加一个按钮来清除所有表格行? 我想将此按钮添加到我的表单中,这是我的代码的fiddle。 我尝试了很多方法,但都没有奏效。
function render(){
for (let i = 0; i < allMovie.length; i++) {
var tr = document.createElement('tr');
tr.setAttribute("id", "mainTR");
table.appendChild(tr);
var td1 = document.createElement('td');
td1.textContent = allMovie[i].MovieName11;
td1.setAttribute("class", "td1");
tr.appendChild(td1);
var td2 = document.createElement('td');
td2.textContent = allMovie[i].selectPlatform11;
td2.setAttribute("class", "td2");
tr.appendChild(td2);
var td3 = document.createElement('td');
td3.textContent = allMovie[i].randomRate;
td3.setAttribute("class", "td3");
tr.appendChild(td3);
var td4 = document.createElement('td');
td4.textContent = allMovie[i].monthlyPay11;
td4.setAttribute("class", "td4");
tr.appendChild(td4);
var td5 = document.createElement('td');
td5.setAttribute("id", "td5");
td5.innerHTML = `<button onclick=remove()> X </button>`
tr.appendChild(td5);
}
}
function remove() { /line10
var removeOBJ = document.getElementById("mainTR");
return removeOBJ.remove();
}
【问题讨论】:
-
您想要一个清除所有行的按钮,还是希望每行都有一个“清除”按钮,只清除自己的行?
-
不,我想要一个清除所有行的按钮。
-
就像在您的小提琴中一样,您只需将按钮添加到您的表单中,然后在按下侦听器时调用您的函数 clearTable()
标签: javascript arrays function for-loop dom