Home >Web Front-end >JS Tutorial >How to dynamically add and delete table rows in JS_javascript skills

How to dynamically add and delete table rows in JS_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 15:12:301322browse

The example in this article describes the method of dynamically adding and deleting table rows using JS. Share it with everyone for your reference, the details are as follows:

function insertRow(tableName,className,bgcolor, cellContentArray){
 var t = document.getElementByIdx(tableName); //取得table表
 var tr = t.insertRow();  //插入一行
 tr.className=className;  //设置行的css
 tr.bgcolor=bgcolor;   //设置行的背景颜色
 for( var i=0;i<cellContentArray.length;i++){
 var td = tr.insertCell(); //插入一列
 td.innerHTML=cellContentArray[i]; //设置列内容
 }
}
function deleteRow(tableName,btn){
 var t = document.getElementByIdx(tableName); //取得table表
 var tr = btn.parentNode.parentNode; //取得对应的行
 t.deleteRow(tr.rowIndex);
}

Readers who are interested in more JavaScript-related content can check out the special topics on this site: "Summary of JavaScript search algorithm techniques", "Summary of JavaScript animation special effects and techniques", "Summary of JavaScript errors and debugging techniques", "Summary of JavaScript data structures and algorithm techniques", "Summary of JavaScript traversal algorithms and techniques" and "JavaScript Mathematics Summary of operation usage

I hope this article will be helpful to everyone in JavaScript programming.

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