说我有以下内容:
如何使用jQuery在第一个之后选择所有子元素?所以我可以实现以下目标:
twernt.. 134
您应该能够使用"not"和"first child"选择器.
$("li:not(:first-child)").addClass("something");
http://docs.jquery.com/Selectors/not
http://docs.jquery.com/Selectors/firstChild
您应该能够使用"not"和"first child"选择器.
$("li:not(:first-child)").addClass("something");
http://docs.jquery.com/Selectors/not
http://docs.jquery.com/Selectors/firstChild
根据我对这四种方法的完全不科学的分析,看起来它们之间没有太大的速度差异.我在包含一系列不同长度的无序列表的页面上运行每个列表,并使用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毫秒
http://docs.jquery.com/Traversing/slice
$("li").slice(1).addClass("something");
更优雅的查询(选择以li元素开头的所有li元素):
$('ul li+li')