2013-04-10 174 views
-1

我有一个tr表标签。我想在它的类中并排显示两个tr标签。我怎么能够使用jQuery来做到这一点。如何显示两个tr的id与id在同一行

<table> 
    <tr class='1'> 
    <td>First</td> 
    </tr> 
    <tr class='1'> 
     <td>second</td> 
    </tr> 
    <tr class='2'> 
     <td>third</td> 
    </tr> 
    <tr class='3'> 
     <td>fourth</td> 
    </tr> 
    <tr class='3'> 
     <td>fifth</td> 
    </tr> 
</table> 

然后在输出我想显示

First  Second 
Third 
Fourth Fifth 

我要设置这些动态我怎么能做到这一点使用jQuery或JavaScript。我想使用为tr标签声明的类。我知道要使用<td>标签。 任何帮助表示赞赏

+0

ID是唯一的!您需要使用班级或不同的名称 – Eli 2013-04-10 10:06:26

+0

您不能对不同的元素使用相同的ID。 – IndoKnight 2013-04-10 10:06:56

+3

但是......行是行。为什么你会故意用没有正确格式的行写html,然后用JS改变它? (我想这是我必须指出的ID应该是唯一的。) – nnnnnn 2013-04-10 10:07:05

回答

1

下面是浮现在脑海的第一种方式:

var $tds = $("td");  // get all the tds 
[].reverse.call($tds); // reverse the order so we can easily loop backwards 

$tds.each(function() { 
    var $parentRow = $(this).parent(), 
     // find the first row with the same class that isn't this row 
     $firstTrSameClass = $("tr").filter("." +$parentRow.attr("class")) 
            .not($parentRow).first(); 
    if ($firstTrSameClass.length > 0) { // if there is such a row 
     $firstTrSameClass.append(this); // move the td 
     $parentRow.remove();   // delete the original row 
    } 
}); 

演示:http://jsfiddle.net/pgcdq/