jquery - 使用 jQuery 将 <td> 替换为 <tr><td>

标签 jquery html

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

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

根本没用。我试着玩 replaceWithreplaceAll , 但没能成功。 问题: 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/

相关文章:

html - 如何让文字在图片上居中?

javascript - 帮我解决 "or"jquery 选择器?

javascript - <span> 在第二行之后换行

javascript - 当按下 'Done' 按钮时,datetimepicker 触发一个 javascript 函数

jquery - 子元素在变化时溢出父边界半径

javascript - 表单验证在 Chrome 中有效,但在 Firefox 中无效

javascript - jQuery .find() 返回 "div"的值,但不返回 "main"元素的值

javascript - 如何添加类名与无序列表 ID 相同的列表项?

javascript - 如何使用 API 从多个 IMDB id 获取电影信息?

html - 如何使用 CSS 制作漫画无 MS 文本 Logo ?