html css li第二个,html - 为最后的<li>更改CSS

html - 为最后的

更改CSS

我想知道是否有一些方法可以使用CSS更改列表中最后一个li的CSS属性。 我已经研究过使用:last-child,但这看起来真的很麻烦,我无法让它为我工作。 如有必要,我将使用JavaScript来执行此操作,但我想知道是否有人可以在CSS中考虑解决方案。

10个解决方案

98 votes

last-child实际上是唯一一种不修改HTML的方法 - 但假设你能做到这一点,主要选择就是给它一个class="last-item",然后做:

li.last-item { /* ... */ }

显然,您可以使用您选择的动态页面生成语言自动执行此操作。 此外,W3C DOM中还有一个last-child JavaScript属性。

这是一个在Prototype中做你想做的事情的例子:

$$("ul").each(function(x) { $(x.lastChild).addClassName("last-item"); });

甚至更简单:

$$("ul li:last-child").each(function(x) { x.addClassName("last-item"); });

在jQuery中,您可以更紧凑地编写它:

$("ul li:last-child").addClass("last-item");

另请注意,这应该可以在不使用实际的last-child CSS选择器的情况下工作 - 而是使用它的JavaScript实现 - 因此它应该在整个浏览器中减少错误并且更可靠。

Lucas Jones answered 2019-06-09T20:38:41Z

46 votes

我用纯CSS完成了这个(可能是因为我来自未来 - 比其他人晚了3年:P)

假设我们有一个清单:

然后我们可以轻松地将最后一项的文本设为红色:

ul#nav li:last-child span {

color: Red;

}

pano answered 2019-06-09T20:39:19Z

11 votes

我通常将CSS和JavaScript方法结合起来,因此它在所有浏览器中都可以在没有JavaScript的情况下运行,但是在IE6 / 7和IE6 / 7中使用JavaScript(但不是关闭),因为它们不支持:last-child伪类。

$("li:last-child").addClass("last-child");

li:last-child,li.last-child{ /* ... */ }

answered 2019-06-09T20:39:44Z

9 votes

您可以使用jQuery并以这种方式执行

$("li:last-child").addClass("someClass");

Keith Adler answered 2019-06-09T20:40:08Z

5 votes

IE7 +和其他浏览器的一种替代方法可能是使用:first-child,并反转您的样式。

例如,如果您在每个:first-child上设置保证金:

ul li {

margin-bottom: 1em;

}

ul li:last-child {

margin-bottom: 0;

}

你可以用这个替换它:

ul li {

margin-top: 1em;

}

ul li:first-child {

margin-top: 0;

}

这适用于其他一些情况,如边框。

根据sitepoint,:first-child越野车,但仅限于它将选择一些根元素(doctype或html),并且如果插入其他元素,则可能不会更改样式。

DisgruntledGoat answered 2019-06-09T20:41:00Z

3 votes

我通常通过创建一个htc文件(例如last-child.htc)来做到这一点:

function initializeBehaviours() {

this.lastChild.className += ' last-child';

}

并从我的IE条件css文件中调用它:

ul { behavior: url("/javascripts/htc/last-child.htc"); }

在我的主要css文件中,我得到了:

ul li:last-child,

ul li.last-child {

/* some code */

}

使用您现有的css标记而不定义任何.last-child类的另一个解决方案(尽管速度较慢)将是Dean Edwards ie7.js库。

vise answered 2019-06-09T20:41:52Z

3 votes

2015答案:CSS last-of-type允许您设置最后一个项目的样式。

ul li:last-of-type { color: red; }

mikemaccana answered 2019-06-09T20:42:17Z

2 votes

:last-child是CSS3并且没有IE支持,而:first-child是CSS2,我相信以下是使用jquery实现它的安全方法

$('li').last().addClass('someClass');

Lawrence answered 2019-06-09T20:42:42Z

2 votes

$('礼')最后()addClass('SomeClass的')。;

如果你有多个

组它只会选择最后一个李。

jaeson answered 2019-06-09T20:43:19Z

0 votes

如果你知道你正在查看列表中有三个li,例如,你可以这样做:

li + li + li { /* Selects third to last li */

}

在IE6中,您可以使用表达式:

li {

color: expression(this.previousSibling ? 'red' : 'green'); /* 'green' if last child */

}

我建议使用专门的类或Javascript(不是IE6表达式),直到:last-child选择器获得更好的支持。

strager answered 2019-06-09T20:44:01Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<html> <head> <meta charset="utf-8" /> <title>星星国际影城</title> <link href="css/index.css" rel="stylesheet" type="text/css" /> <script src="js/main.js" type="text/javascript"></script> </head> <body> <!--顶部--> <div id="header"> <div class="main"> <div class="info"> <a href=" " class="sina" title="sina" alt="新浪"></a > <a href="#" class="tqq" title="tqq" alt="tqq"></a > <a href="login.html" target="_blank">登录|</a > <a href="sign.html" target="_blank">注册|</a > <span>全国服务热线:888-8888888</span> </div> </div> </div> <!--logo部分--> <div id="logo"> <div class="logo_star"> <a href="index.html">< img src="images/logo.jpg" title="" alt="logo" /></a > </div> </div> <!--导航--> <div id="nav"> <div class="menu"> <!--导航菜单--> <ul> <li><a href="#" class="active">首页</a ></li> <li><a href="#">影片</a ></li> <li><a href="#">影城</a ></li> <li><a href="#">购票</a ></li> <li><a href="#">影城活动</a ></li> <li><a href="#">会员服务</a ></li> <li><a href="#">顾客互动</a ></li> </ul> <!--搜索框--> <div class="search"> <input type="text" placeholder="搜索影片" name="search"/> <input type="button" name="btn" value="" /> </div> </div> </div> <!--主体部分--> <div id="main"> <!--banner轮播图--> <div class="banner"> <ul class="main_content"> <li>< img src="images/b1.png" /></li> <li>< img src="images/b2.png" /></li> <li>< img src="images/b3.png" /></li> </ul> <!--轮播切换--> <ul class="tab"> <li></li> <li></li> <li></li> </ul> </div> <!--快速购票--> 在每行代码进行解释
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值