jquery:选择列表中的最后4个项目 [英] jquery: select the last 4 items in the list

查看:87
本文介绍了jquery:选择列表中的最后4个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图片/文字连结清单,

 < ul> 
< li>链接1< / li>
< li> link 2< / li>
< li> link 3< / li>
< li> link 4< / li>

< li> link 5< / li>
< li>链接6< / li>
< li> link 7< / li>
< li> link 8< / li>
< / ul>

而且我的样式是我的.css文件中的列表,

  li {
float:left;
margin:0px 5px 5px 0px;
}

但我想要显示列表中最后4个项目边距底部为5px空间。所以我想使用jqeury选择最后4个项目,并改变他们的CSS样式为marginBottom:0px;



问题是这是如何jquery选择最后一项,

  li:last 

但最后4个呢?

解决方案

<$

p $ p> $('li')。slice(-4).css('color','red')

示例


I have a list of images/ text links,

<ul>
<li>link 1</li>
<li>link 2</li>
<li>link 3</li>
<li>link 4</li>

<li>link 5</li>
<li>link 6</li>
<li>link 7</li>
<li>link 8</li>
</ul>

and I style the list in my .css file,

li {
  float:left;
  margin:0px 5px 5px 0px; 
}

but then I want the last 4 items in the list to be displayed without the 5px space at their margin bottom. so I would like to use jqeury to select these last 4 item and change their css style to marginBottom:0px;

the problem is that this is how jquery select the last item,

li:last

but what about the last 4? or maybe the last 5, or 6?

thanks.

解决方案

$('li').slice(-4).css('color', 'red')

Example

这篇关于jquery:选择列表中的最后4个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆