jquery - 获取除第一个和最后一个之外的所有行

时间:2010-02-12 17:15:55

标签: jquery jquery-selectors

我想动态地将一个类添加到除第一行和最后一行之外的表的所有行。如果没有为行分配css类来识别它们,我将如何做到这一点。我正在使用

获得除第一行之外的所有行
$("#id").find("tr:gt(0)")

我需要以某种方式将其与not("tr:last")结合起来吗?

6 个答案:

答案 0 :(得分:94)

删除gt(),因为我认为它比:first慢一点。

not():first:last结合使用:

$('table#tbl > tbody > tr').not(':first').not(':last').addClass('highlight');

大多数浏览器会自动在表标记中添加tbody元素(如果缺少),这就是直接子选择器失败的原因 - 没有<tr>个元素作为{{1的直接子元素标签。

我不是100%确定这是所有浏览器的方式,因此手动添加<table>会更安全。否则你需要一点点嗅探,不能作为一个衬里:

<tbody>

希望这能解决你的问题!

答案 1 :(得分:14)

试试这个:

.not(':first').not(':last')

答案 2 :(得分:11)

为什么不呢?

$('table tr:not(:first-child):not(:last-child)');

也可以作为纯CSS选择器使用。

答案 3 :(得分:4)

您可以通过用逗号分隔选择器将.not()方法合并为一个:

$('#id tr').not(':first, :last');
$('#id tr:not(:first, :last');

请注意,第二个在纯CSS中无效,仅作为jQuery选择器。对于纯CSS,您必须使用@Sumit的答案。

答案 4 :(得分:2)

奇怪的是发布的建议没有用,他们应该全部工作!但是...

如果它不起作用,就这样做......稍微慢但是必须工作!! 尝试:

$('table#tbl tr').addClass('highlight');
$('table#tbl tr:first-child').removeClass('highlight');
$('table#tbl tr:last-child').removeClass('highlight');

答案 5 :(得分:2)

您还可以使用.slice() method删除集合中的第一个/最后一个元素:

if (loggedIn) 
{ 
    DocuSign.Integrations.Client.Template template = new DocuSign.Integrations.Client.Template(); 

    template.Login = _account; 

    List roles = new List(); 
    TemplateRole templateRoleSubscriber = new TemplateRole(); 
        templateRoleSubscriber.roleName = "Subscriber"; 
        templateRoleSubscriber.routingOrder = "1"; 
        templateRoleSubscriber.defaultRecipient = "true"; 
    roles.Add(templateRoleSubscriber); 

    TemplateRole templateRoleApprover = new TemplateRole(); 
        templateRoleApprover.roleName = "Approver"; 
        templateRoleApprover.routingOrder = "2"; 
        templateRoleApprover.defaultRecipient = "true"; 
    roles.Add(templateRoleApprover); 

    template.TemplateRoles = roles.ToArray(); 

    if (template.CreateTemplate(new List(), new List(), "New Template")) 
    { 
        //Save Template in our database as a 'linked' document 
        Template.DocuSignTemplateId = template.EnvelopeId; 
        Template = _SignatureRepo.SaveTemplate(Template); 

       //Load DocuSign Console to finish editing template. 
       _account.GetUserConsoleView(); 
       return _account.ConsoleUrl; 
    }
}

.slice() method本质上创建了一个新的jQuery对象,其中包含初始集中指定的元素子集。在这种情况下,它将排除索引为$('table tr').slice(1, -1); 1的元素,这些元素分别是第一个/最后一个元素。

实施例

&#13;
&#13;
-1
&#13;
$('table tr').slice(1, -1).css('background-color', '#f00');
&#13;
table { width: 100%; }
&#13;
&#13;
&#13;

当然,您也可以使用<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr><td>1</td></tr><tr><td>2</td></tr> <tr><td>3</td></tr><tr><td>4</td></tr> <tr><td>5</td></tr><tr><td>6</td></tr> </table>否定:first-child / :last-child

&#13;
&#13;
:not()
&#13;
$('table tr:not(:first-child):not(:last-child)').css('background-color', '#f00');
&#13;
table { width: 100%; }
&#13;
&#13;
&#13;

您还可以合并<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr><td>1</td></tr><tr><td>2</td></tr> <tr><td>3</td></tr><tr><td>4</td></tr> <tr><td>5</td></tr><tr><td>6</td></tr> </table>:nth-child(n+2)

&#13;
&#13;
:nth-last-child(n+2)
&#13;
$('table tr:nth-child(n+2):nth-last-child(n+2)').css('background-color', '#f00');
&#13;
table { width: 100%; }
&#13;
&#13;
&#13;