Add table cell to existing table row, jQuery

2019-04-10 02:22发布

问题:

I'm trying to add values to a table using jQuery - unfortunately, I don't know how to get jQuery to add table cells to an existing row. For example:

$("<td><a href='#'>" + key + "</a></td>").click(function(e) {
    e.preventDefault();
    testset(key);
}).appendTo('#table1');

This adds cells to the end of the table with id table1. What would be the best way to go about adding cells to an existing table row (<tr>) using jQuery? Quick google hasn't revealed anything. Regards,

SystemError

回答1:

.appendTo('#table1 #rowId');

Or you could do:

.appendTo('#table1 tr:nth-child(5)');

http://api.jquery.com/nth-child-selector/



回答2:

You must append them to a specific row and not to the table:

$("<td><a href='#'>" + key + "</a></td>").click(function(e) {
    e.preventDefault();
    testset(key);
}).appendTo('#table1 tr#yourrowId');