当前位置:  开发笔记 > 编程语言 > 正文

选择除第一个之外的所有子元素

如何解决《选择除第一个之外的所有子元素》经验,为你挑选了4个好方法。

说我有以下内容:

  • First item
  • Second item
  • Third item

如何使用jQuery在第一个之后选择所有子元素?所以我可以实现以下目标:

  • First item
  • Second item
  • Third item

twernt.. 134

您应该能够使用"not"和"first child"选择器.

$("li:not(:first-child)").addClass("something");

http://docs.jquery.com/Selectors/not

http://docs.jquery.com/Selectors/firstChild



1> twernt..:

您应该能够使用"not"和"first child"选择器.

$("li:not(:first-child)").addClass("something");

http://docs.jquery.com/Selectors/not

http://docs.jquery.com/Selectors/firstChild



2> twernt..:

根据我对这四种方法的完全不科学的分析,看起来它们之间没有太大的速度差异.我在包含一系列不同长度的无序列表的页面上运行每个列表,并使用Firebug探查器对它们进行计时.

$("li").slice(1).addClass("something");

平均时间:5.322ms

$("li:gt(0)").addClass("something");

平均时间:5.590毫秒

$("li:not(:first-child)").addClass("something");

平均时间:6.084毫秒

$("ul li+li").addClass("something");

平均时间:7.831毫秒


我遇到了这个问题因为我需要得到所有的孩子,但第一个.我在div中的所有h3上做这个.我在我的页面上运行了上面的三个查询,然后离开了:不是(:first-child)最快2-3次.我认为页面结构的方式也可能会影响配置文件的速度.无论哪种方式,这个答案教我如何使用Firebug探测器,我感谢你.
谢谢你这样做!

3> Jonny Buchan..:

http://docs.jquery.com/Traversing/slice

$("li").slice(1).addClass("something");



4> Sergey Ilins..:

更优雅的查询(选择以li元素开头的所有li元素):

$('ul li+li')

推荐阅读
惬听风吟jyy_802
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有