jquery添加并添加

如何使用jquery添加

问题是我的表有1或2行?

 $('#myTable tr:has(th)').wrap(''); 

 
1234
1234
1234
1234
1234
1234
1234

您需要做的是删除行并将它们附加到thead元素

 var myTable = jQuery("#myTable"); var thead = myTable.find("thead"); var thRows = myTable.find("tr:has(th)"); if (thead.length===0){ //if there is no thead element, add one. thead = jQuery("").appendTo(myTable); } var copy = thRows.clone(true).appendTo("thead"); thRows.remove(); 

jsFiddle exmaple

使用wrapAll而不是wrap

 $('#myTable tr:has(th)').wrapAll('');​ $("#myTable thead").prependTo("#myTable") 
 function createTable(data) { var str = ""; str += ''; str += ''; for (var item in data.recentList) { str += ''; for (idata in data.recentList[item]) { str += ''; } str += ''; } str += '
PosRef
' + data.recentList[item][idata] + '
'; $('body').append(str); }

从数组创建表的工作版本