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

How to delete rows from table in javascript

藏色散人
藏色散人Original
2021-07-01 10:14:293124browse

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)

43c4707103c6cc3650c2aeb71d2b1e8d Commonly used functions in: tableObject.insertRow(index), tableObject.deleteRow(index)

43c4707103c6cc3650c2aeb71d2b1e8d Common properties: tableObject.rows, tableObject.rows.length

868c3f8d39a371013a78a87c30e1b0d8 Commonly used functions: tablerowObject.insertCell(index)

868c3f8d39a371013a78a87c30e1b0d8 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>

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!

Statement:
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