如何将id属性设置为表?

时间:2017-04-01 06:29:59

标签: javascript jquery html

我想用javascript创建一个带有我的id的表到我的表,我正在使用setAttribute但它不起作用,你能帮我吗?

var table = $('<table><thead><tr><th>Name</th><th>Age</th><th>Position</th><th>Note</th></tr></thead><tbody></tbody></table>').addClass('table table-bordered table-hover search-table table-striped');
table.setAttribute("id", "myid");
$('#content').html(table);

1 个答案:

答案 0 :(得分:3)

由于table是一个jQuery对象,您需要使用.attr(attributeName, value)

table.attr("id", "myid");
相关问题