开发者

Unable to create table in jQuery

开发者 https://www.devze.com 2022-12-09 03:48 出处:网络
I\'ll post the two solutions I have tried, and what failed with each one: First: var table = doc开发者_如何学Pythonument.createElement(\"table\");

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消