简体   繁体   中英

Clear content of cell with JavaScript

I want to dynamically change the content of the cells of an entire column. Currently I loop over the rows and append my element.

for (var i = 0, row; row = table.rows[i]; i++) {
            var cell = row.cells[1];
            cell.appendChild(element.cloneNode(true))

This works fine. But the problem is, if I call this function again, an additional child is added. So each time I call the function an element is added. Is there a way to clear the content of the cell before I append my element or a way to update the content of the cell?

try this:

while (cell.hasChildNodes()) {
    cell.removeChild(cell.lastChild);
}

Try this:

for (var i = 0, row; row = table.rows[i]; i++) {
            var cell = row.cells[1];
            cell.innerHTML="";
            cell.appendChild(element.cloneNode(true))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM