我在这里做一个懒惰的表转置。 我有一个这样的表:
<table>
<th style="text-align: left; text-transform: capitalize;">Unique</th>
<th style="text-align: left; text-transform: capitalize;">x1</th>
<th style="text-align: left; text-transform: capitalize;">y2</th>
<tr class="rowNormal">
<td nowrap="true">a1</td>
<td nowrap="true">b2</td>
<td nowrap="true">b3</td>
</tr>
</table>
我需要看起来像这样:
<table>
<tr class="rowNormal">
<tr><td nowrap="true">a1</td><.tr>
<tr><td nowrap="true">b2</td></tr>
<tr><td nowrap="true">b3</td></tr>
</tr>
</table>
它只有两行,其中一行是标题。 所以我隐藏标题是这样的:
$("table:contains('Unique')").find("th").addClass("hidden");
效果很好。现在我需要添加一个 <tr>
每个 <td nowrap="true">
,在这里我遇到了麻烦。我用这个:
$('div.content').html($('div.content').html().replace(/<td nowrap="true">/g,' <tr><td nowrap="true">'));
这有效,但我想将其缩小到包含“唯一”的表,而不使用全局。 这个
$('div.content').html($('div.content').html().replace(/</td>/g,'<td></tr>'));
根本没用。我试着玩 replaceWith
和 replaceAll
, 但没能成功。
问题:
1)添加格式以将剩余行转置为列的最佳方法?
2) 如何指定 "
和 /
作为参数?
3)'
之间有什么区别?和 "
?
感谢您的宝贵时间。
最佳答案
您可以使用 wrap
在 jQuery 中这样做:
$("td[nowrap='true']").wrap("tr");
或者对于仅包含您的 Unique 包含选择器的项目:
$("table:contains('Unique') td[nowrap='true']").wrap("tr");
关于jquery - 使用 jQuery 将 <td> 替换为 <tr><td>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15090320/