Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 删除动态创建的表行_Javascript_Html_Html Table_Onclick - Fatal编程技术网

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”;“///我应该将其移出函数本身,或者最终在函数内移动它?这两种方式都不会删除行////