如何在同一行中显示具有其id的两个tr's
How to display two tr's with its id in the same row by its id
我有一个带有tr标签的表。我想在它的类旁边并排显示两个tr标记。如何使用jquery来实现呢?
<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>
标签。
我想到的第一种方法是:
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/
我真的看不到这里的用例,但我想最简单的是创建新行并将单元格移动到它们。你可以在这里找到有关移动元素的信息:如何将一个元素移动到另一个元素中?
相关文章:
- 当同一浏览器的两个实例浏览时,Javascript页面如何具有唯一的ID
- JavaScript:当有两个B类元素时,如何在id X中的B类中选择A类
- 为两个ID设置一个变量的正确语法
- 如果两个id具有相同的值,则添加类
- 如何使用js同时更改两个ID的类名
- 如何使用 javascript 在 HTML 元素的两个 ID 之间切换
- 如何在javascript AJAX中从php获取两个id值
- 如何在 JS 中匹配两个 ID 时使用忽略大小写
- JS:如何获得两个ID
- Sequelize-两个ID上的双联接表
- JS Filter函数删除数组中两个ID's在对象中
- jquery针对两个id的单个var
- 如何在一个文档中使用两个id.getElementById类型函数
- 如何在单击事件上传递来自两个ID的两个变量
- 循环遍历两个id内的所有元素
- 需要展开/折叠游戏摘要,并使用交换机连接两个id
- 遍历对象以查找两个ID变量的匹配项
- 对单个Jquery插件使用两个ID
- 我如何在两个html元素之间交换两个id's
- 为self.location设置两个id的值