Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/363.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_Rows - Fatal编程技术网

如何在使用Javascript删除某一行时更新表

如何在使用Javascript删除某一行时更新表,javascript,html,rows,Javascript,Html,Rows,我想在删除表中的行时更新索引。 例如,我删除了第1行,那么第2行和第3行应该变成第1行和第2行,依此类推 function deleteRow(tableID) { try { var table = document.getElementById(tableID); var rowCount = table.rows.length; for(var i=0; i<rowCount; i++)

我想在删除表中的行时更新索引。 例如,我删除了第1行,那么第2行和第3行应该变成第1行和第2行,依此类推

function deleteRow(tableID) {
            try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[0];
                if(null != chkbox && true == chkbox.checked) {
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }


            }
            }catch(e) {
                alert(e);
            }
        }
函数deleteRow(tableID){
试一试{
var table=document.getElementById(tableID);
var rowCount=table.rows.length;

对于(var i=0;i您不需要做任何事情!当从dom中删除行时,行索引会自动更改


如果您想仔细检查,可以钩住一个mutationevent DOMNodeRemoved并查看发生了什么,或者在删除后保留一个断点并验证行计数和索引。

以下是一个完整的jQuery解决方案:

测试它。只需单击一行即可删除它

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js""></script>

    <script>
        $(function () {

            $('tr').click(function () {

                $(this).remove()
                recountRows();
            });


            var recountRows = function () {
                var index = 1;

                $('.index').each(function () {
                    $(this).html(index);
                    index++;
                });
            }


        });
    </script>
</head>
<body>
    <table>
        <tr>
            <td class="index">1</td><td>table text</td>
        </tr>
        <tr>
            <td class="index">2</td><td>table text</td>
        </tr>
        <tr>
            <td class="index">3</td><td>table text</td>
        </tr>
        <tr>
            <td class="index">4</td><td>table text</td>
        </tr>
        <tr>
            <td class="index">5</td><td>table text</td>
        </tr>
        <tr>
            <td class="index">6</td><td>table text</td>
        </tr>
        <tr>
            <td class="index">7</td><td>table text</td>
        </tr>
        <tr>
            <td class="index">8</td><td>table text</td>
        </tr>


    </table>


</body>
</html>


你能给出一个表格的HTML格式的例子吗?