jQuery:克隆表及其第一行 [英] Jquery: cloning a table and its first row

查看:103
本文介绍了jQuery:克隆表及其第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的标准表结构:

I have a standard table structure like this:

<table id="test">
  <th>...</th>
  <tr><td>one thing</td></tr>
  <tr><td>another</td></tr>
  ...
  ...
</table>

我知道如何克隆整个表或表的第n行,但是我需要的是整个表,除了表的第2、3、4等行,换句话说:

I know how to clone the entire table, or the nth row of a table, but what I need is the whole thing, EXCEPT the 2nd, 3rd, 4th etc row of the table, in other words:

<table id="test">
  <th>...</th>
  <tr><td>one thing</td></tr>
</table>

好主意吗?

推荐答案

我知道如何克隆整个表格..."

然后执行此操作,克隆整个表,然后删除不需要的内容.

Then do that, clone the entire table, then remove what you dont need.

选择除第一个孩子以外的所有东西的技巧是使用*+*. CSS中的+选择器表示右侧的匹配元素,紧随左侧的元素.

A trick to select everything except first child is to use *+*. + selector in css mean the matching element on right side next to the element on the left side.

最后,您可以使用它:

var $clone = $('#test').clone().find('tr+tr').remove().end();

$clone将成为仅第一行的克隆表.

$clone will then be your clone table with only the first row.

这篇关于jQuery:克隆表及其第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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