Home > Web Front-end > JS Tutorial > How to delete rows from table in javascript

How to delete rows from table in javascript

藏色散人
Release: 2021-07-01 10:14:29
Original
3233 people have browsed it

How to implement table deletion in javascript: first create an HTML sample file; then define a table; finally, use the "function deleteRow(_this){...}" method to implement the function of deleting rows. .

How to delete rows from table in javascript

The operating environment of this article: windows7 system, javascript version 1.8.5, DELL G3 computer

How to delete rows from table in javascript?

JavaScript dynamically adds and deletes rows for the table

I have written a total of three methods to implement the function of adding rows, and one method to implement the function of deleting rows, mainly It feels like you must be familiar with JavaScript APIs. In fact, these things are included in the API documents. It depends on whether you know that there are functions or properties in JavaScript, and then integrating these properties and functions will give you what you want.

Common functions that can be used by all HTML elements: node.appendChild(node),

Common attributes that can be used by all HTML elements: element.tagName

document object Commonly used methods: document.createElement(name)

< table> Commonly used functions in: tableObject.insertRow(index), tableObject.deleteRow(index)

< table> Common properties: tableObject.rows, tableObject.rows.length

< tr> Commonly used functions: tablerowObject.insertCell(index)

< tr> Commonly used properties: tablerowObject .rowIndex

<style type="text/css">
    table{
        border:1px solid #000;
        border-collapse: collapse;
    }
    th,td{
        border:1px solid #000;
        padding:6px;
    }
</style>
<script type="text/javascript">
    function addRow1(){
        var userInfo = document.getElementById("userInfo");
        var row = document.createElement("tr");
        var td1 = document.createElement("td");
        td1.innerHTML = "李四";
        var td2 = document.createElement("td");
        td2.innerHTML = "102";
        var td3 = document.createElement("td");
        td3.innerHTML = "北海";
        var td4 = document.createElement("td");
        td4.innerHTML = "<a onclick=&#39;delete(this)&#39;>删除</a>";
        row.appendChild(td1);
        row.appendChild(td2);
        row.appendChild(td3);
        row.appendChild(td4);
        userInfo.appendChild(row);              
    }
    function addRow2(){
        var userInfo = document.getElementById("userInfo");
        var rowLength = userInfo.rows.length;
        //新行将被插入index所在行之前。若index等于表中的行数,则新行将被附加到表的末尾。
        //返回一个TableRow,表示新插入的行。
        var tableRow = userInfo.insertRow(rowLength);
        tableRow.innerHTML = "<td>李四</td><td>102</td><td>四海</td><td><a onclick=&#39;deleteRow(this)&#39;>删除</a></td>";
    }
    function addRow3(){
        var userInfo = document.getElementById("userInfo");
        //计算rows.length时会把表头包含在内
        var rowLength = userInfo.rows.length;
        var tableRow = userInfo.insertRow(rowLength);
        //新单元格将被插入当前位于 index 指定位置的表元之前
        //如果 index 等于行中的单元格数,则新单元格被附加在行的末尾。
        var tableCell0 = tableRow.insertCell(0);
        var tableCell1 = tableRow.insertCell(1);
        var tableCell2 = tableRow.insertCell(2);
        var tableCell3 = tableRow.insertCell(3);
        tableCell0.innerHTML = "李四";
        tableCell1.innerHTML = "103";
        tableCell2.innerHTML = "黑海";
        tableCell3.innerHTML = "<a onclick=&#39;deleteRow(this)&#39;>删除</a>";
    }
    //不能传初始化时当前元素所在行的rowIndex,因为删除操作之后当前元素所在行的rowIndex会发送变化
    function deleteRow(_this){
        var userInfo = document.getElementById("userInfo");
        var rowIndex = getTrIndex(_this);
        //deleteRow() 方法用于从表格删除指定位置的行
        //参数 index 指定了要删除的行在表中的位置
        userInfo.deleteRow(rowIndex);
    }
    function getTrIndex(element){
        if(element.tagName.toLowerCase() == "tr"){
            //rowIndex属性返回某一行在表格的行集合中的位置(row index)
            return element.rowIndex;
        }else{
            return getTrIndex(element.parentNode);
        }
    }
</script>
Copy after login

Recommended study: "javascript Advanced Tutorial"

The above is the detailed content of How to delete rows from table in javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template