简体   繁体   English

以前为jQuery的兄弟选择器

[英]Previous siblings selectors for jQuery

If I have a simple HTML list 如果我有一个简单的HTML列表

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li id="some-id">Item 3</li>
  <li>Item 4</li>
  <li>Item 5</li>
</ul>

it's easy to select every list item after #some-id : #some-id 之后很容易选择每个列表项:

$("#some-id ~ li")

but how do I select the items before #some-id ? 但如何 #some-id 之前选择项目?

Use .prevAll() , like this: 使用.prevAll() ,如下所示:

$("#some-id").prevAll()

For example: 例如:

$("#some-id").prevAll().css('color', 'red')​​​​​​​​​​​;​

Give it a try here , there's not a "previous siblings" selector like your next-siblings selector , but .prevAll() will get the elements you want, the same as you could substitute your current selector with $("#some-id").nextAll() . 在这里尝试一下 ,没有像你的下一个兄弟选择器那样的“兄弟姐妹”选择 ,但是.prevAll()将获得你想要的元素,就像用$("#some-id").nextAll()替换你当前的选择器一样$("#some-id").nextAll()

$("#some-id").prevAll()

请参阅文档: http//api.jquery.com/prevAll/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM