如何使用jQuery将一个表行分为两个表行 [英] How can I break a table row into two table rows using jQuery

查看:100
本文介绍了如何使用jQuery将一个表行分为两个表行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张宽桌子,我想将每行分成两行,在每个带有"column-6"类的单元格之后.

I have a wide table that I would like to split each row into two rows, after each cell with the class "column-6".

$( ".column-6" ).after( "</tr><tr class='secondary'>" );

由于某些原因,它包含为:

For some reason it is included as:

<tr class="secondary"></tr>

JS Fiddle示例: http://jsfiddle.net/xa9nk/1/

JS Fiddle example: http://jsfiddle.net/xa9nk/1/

推荐答案

我认为您应该更改tablehtml.首先,您必须在所需位置插入令牌,然后用所需的字符串(</tr><tr class='secondary'>)执行令牌替换:

I think you should change the html of the table. First you have to insert the token at the positions you want, then perform the replacement of the token with the string you want (which is </tr><tr class='secondary'>):

var yourOtherFace = jQuery.noConflict();
var token = "sdfdskjflsfj34234ksldjfskdl";

yourOtherFace( ".column-6" ).after(token)
 .closest('table').html(function(i,oldHtml){
     return oldHtml.replace(new RegExp(token,"g"), "</tr><tr class='secondary'>");     
 });

yourOtherFace('.column-1').attr('rowspan', '2');

演示.

这是另一个使用纯正则表达式的解决方案,它没有先插入token,它可能会更短,但是我做了一个测试用例,看起来比第一个解决方案(使用token)慢了约40%.

Demo.

Here's another solution using pure Regex without inserting some token first, it may be shorter but I've made a test case and looks like it's about 40% slower than the first solution (using token).

演示2

这是性能测试.

这篇关于如何使用jQuery将一个表行分为两个表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆