Javascript 如何将id属性设置为表?

Javascript 如何将id属性设置为表?,javascript,jquery,html,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>

我想用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);
var-table=$('NameAgePositionNote').addClass('table-table-bordered-table-hover-search-table-table-striped');
表.setAttribute(“id”、“myid”);
$('#content').html(表格);

由于
是一个jQuery对象,您需要使用


由于
table
是一个jQuery对象,您需要使用

table.attr("id", "myid");