Javascript 删除动态创建的表行
我正在尝试以下函数来创建表行并在单击函数时删除它们,以下是我尝试的内容:Javascript 删除动态创建的表行,javascript,html,html-table,onclick,Javascript,Html,Html Table,Onclick,我正在尝试以下函数来创建表行并在单击函数时删除它们,以下是我尝试的内容: function CreateDelete() { var table = document.getElementById('newbook'); var tableBody = document.createElement('tbody'); var row, columnUserId, columnCountingAreaID, columnDe
function CreateDelete()
{
var table = document.getElementById('newbook');
var tableBody = document.createElement('tbody');
var row, columnUserId, columnCountingAreaID, columnDeleteRec;
row = document.createElement('tr');
columnUserId = document.createElement('td');
columnUserId.appendChild(document.createTextNode("hi"));
columnCountingAreaID = document.createElement('td');
columnCountingAreaID.appendChild(document.createTextNode("there"));
columnDeleteRec = document.createElement('td');
var element = document.createElement("input");
//Assign different attributes to the element.
element.setAttribute('type','button');
element.setAttribute('name','X');
element.setAttribute('value','X');
element.onclick = function() {
$('#newbook input[type="button"]').click (function () {
$(this).closest('tr').remove();});
};
row.appendChild(columnUserId);
row.appendChild(columnCountingAreaID);
columnDeleteRec.appendChild(element);
row.appendChild(columnDeleteRec);
tableBody.appendChild(row);
table.appendChild(tableBody);
}
问题在于它没有删除行onclick操作。
任何人都能看出这里做错了什么。单击行时,您正在绑定事件
$('#newbook input[type="button"]').click(function () {
$(this).closest('tr').remove();
});
这应该在代码末尾,并删除带有元素的绑定事件。onclick您只需将onclick处理程序更改为:
element.onclick = function() {
$(this).closest('tr').remove();
};
不起作用,即在此行“$(this).closest('tr').remove();”上提示错误“Object expected”;“///我应该将其移出函数本身,或者最终在函数内移动它?这两种方式都不会删除行////