我有一张这样的 table :
<thead>
<tr class="row-2"><tr>
<tr class="row-3">..<tr>
<tr class="row-4">..<tr>
<tr class="row-5">..<tr>
<tr class="row-6">..<tr>
<tr class="row-7"><tr>
<tr class="row-8">..<tr>
<tr class="row-9">..<tr>
<tr class="row-10">..<tr>
<tr class="row-11">..<tr>
</thead>
如何使用 jQuery 将所有 tr 标签替换为 thead 标签内的 th 标签?
最佳答案
获取表格 html,然后replace所有出现的事件或 tr(有关详细信息,请参阅 RegExp)。您可以将 设置为具有 id 并访问它。
这会将整个表中的所有 tr 更改为 th。 编辑
为了获得更好的性能,感谢 jAndy。 .find() 比 $('#mytable > tr') 更快吗? 关于jquery - 如何用 th 标签替换所有 tr 标签,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/4374595/
$('#tableId').html($('#tableId').html().replace(/tr/gi, 'th'));
$('#mytable').find('tr').each(function() {
var $this = $(this);
$this.replaceWith('<th class="' + this.className + '">' + $this.text() + '</th>');
});