3

我在这里做一个懒惰的表格转置。我有一张这样的桌子:

     <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>'));

根本不起作用。我尝试玩replaceWithand replaceAll,但没有成功。问题:1)添加格式以将剩余行转置为列的最佳方法?2)如何指定"/作为参数?'3)和有什么区别"

感谢您的时间。

4

1 回答 1

4

你可以wrap在 jQuery 中使用来做到这一点:

$("td[nowrap='true']").wrap("tr");

或仅适用于您的唯一包含选择器的项目:

$("table:contains('Unique') td[nowrap='true']").wrap("tr");
于 2013-02-26T13:33:48.297 回答