Unable to create table in jQuery
I'll post the two solutions I have tried, and what failed with each one:
First:
var table = doc开发者_如何学Pythonument.createElement("table");
table.addClass("nice");
// fails because table does not have the "addClass" method
Second:
var table = $(document.createElement("table"));
table.addClass("nice");
var row = table.insertRow(-1);
// fails because table does not have the "insertRow" method (it has been cleared by jQuery)
How can I properly create a table and add rows and cells to it using jQuery?
var table = $('<table>')
.addClass('nice');
To add rows, just create elements and append them to the table.
var table = $('<table>').addClass('foo').append( $('<tr>').append( $('<td>').text('lol') ) ).appendTo('body')
It helps storing them in multiple variables, obviously.
精彩评论