zoukankan      html  css  js  c++  java
  • Javascript插入、删除一个表格行

    <html>
    <head>
    <script type="text/javascript">
    function deleteRow(r)
    {
    var i=r.parentNode.parentNode.rowIndex
    document.getElementById('myTable').deleteRow(i)
    }

    function insertRowInTable(){

    <html>
    <head>
    <script type="text/javascript">
    function deleteRow(r)
    {
    var i=r.parentNode.parentNode.rowIndex
    document.getElementById('myTable').deleteRow(i)
    }

    function insertRowInTable(){
    var table = document.getElementById("myTable");
    var newRow = table.insertRow(-1);
    newRow.insertCell().innerHTML = newRow.rowIndex;
    newRow.insertCell(-1).innerHTML = "<input type='button' value='删除' onclick='deleteRow(this)'>";
    }
    </script>
    </head>
    <body>

    <table id="myTable" border="1">
    <tr>
    <td>Row 1</td>
    <td><input type="button" value="删除" onclick="deleteRow(this)"></td>
    </tr>
    <tr>
    <td>Row 2</td>
    <td><input type="button" value="删除" onclick="deleteRow(this)"></td>
    </tr>
    <tr>
    <td>Row 3</td>
    <td><input type="button" value="删除" onclick="deleteRow(this)"></td>
    </tr>
    </table>
    <input type="button" onclick="insertRowInTable();" value="插入一行" />
    </body>
    </html>


    deleteRow()   从表格删除一行。

    insertRow()   在表格中插入一个新行。

    innerHTML     设置或返回行的开始标签和结束标签之间的 HTML。

    rowIndex      返回该行在表中的位置。

    var table = document.getElementById("myTable");
    var newRow = table.insertRow(-1);
    newRow.insertCell().innerHTML = newRow.rowIndex;
    newRow.insertCell(-1).innerHTML = "<input type='button' value='删除' onclick='deleteRow(this)'>";
    }
    </script>
    </head>
    <body>

    <table id="myTable" border="1">
    <tr>
    <td>Row 1</td>
    <td><input type="button" value="删除" onclick="deleteRow(this)"></td>
    </tr>
    <tr>
    <td>Row 2</td>
    <td><input type="button" value="删除" onclick="deleteRow(this)"></td>
    </tr>
    <tr>
    <td>Row 3</td>
    <td><input type="button" value="删除" onclick="deleteRow(this)"></td>
    </tr>
    </table>
    <input type="button" onclick="insertRowInTable();" value="插入一行" />
    </body>
    </html>

  • 相关阅读:
    Tomcat 之 Aio的安装步骤
    redis的安装问题
    Tomcat开机自启
    常用基本SQL语句
    点击开启此虚拟机时,出现“该虚拟机似乎正在使用中”问题
    sql表连接left join,right join,inner join三者之间的区别
    Lua 中 ipairs 与 pairs 的区别
    Lua 栈【转】【学习笔记】
    Nodejs 环境设置
    nodejs取得当前执行路径
  • 原文地址:https://www.cnblogs.com/0000/p/1549529.html
Copyright © 2011-2022 走看看