【问题标题】:Select n:th child from the bottom with jQuery? [duplicate]用jQuery从底部选择第n个孩子? [复制]
【发布时间】:2012-06-23 04:25:43
【问题描述】:

可能重复:
jquery: select the last 4 items in the list

jQuery 有 nth-child 伪类,但有没有办法选择say 3:rd to last child?

【问题讨论】:

  • 嘶嘶声是否删除了对:nth-last-of-type的支持?

标签: javascript jquery


【解决方案1】:

假设您不一定需要使用:nth-child() 表示法,您可以使用带有eq() 的负索引:

$(selector).eq(-3);

JS Fiddle demo.

或者,在兼容的浏览器中,您可以简单地使用 CSS:

elementSelector:nth-last-child(3) {
    /* CSS declaration block */
}

JS Fiddle demo(在 Chromium 19/Ubuntu 11.04 中经过测试并确认可以正常工作)。

CSS 选择器,大概要感谢document.querySelector()/document.querySelectorAll(),也可以作为 jQuery 中的选择器使用:

$('li:nth-last-child(3)').css('background-color','#f90');​

JS Fiddle demo

参考资料:

【讨论】:

  • 据我所知,jQuery 使所有选择器都可以跨浏览器使用,所以我认为这些都可以正常工作!
猜你喜欢
  • 2013-10-22
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 2012-06-13
  • 2023-03-16
  • 2017-03-11
  • 1970-01-01
  • 2015-01-29
相关资源
最近更新 更多