CSS 特异性:最后一个孩子
我想使用以下内容来定位 div 内最后一个 ul 的最后一个链接 (a)。所以这就是我想到的:
#menu ul:last-child li a {
/*.....*/
}
我无法手动向该元素添加一个类,即使我想动态地执行此操作,我仍然必须以上述方式定位该元素。
有什么想法为什么这不起作用吗?
I would like to use the following to target the last link (a) of the last ul inside my div. So this is what came to mind:
#menu ul:last-child li a {
/*.....*/
}
I cant manually add a class to that element, and even if i wanted to do it dynamically i would still have to target the element the above way.
Any ideas why this is not working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想要最后一个链接位于最后一个中(假设链接位于
li
元素内),那么这就是您想要的:ul:last-child
返回#menu
中的最后一个ul
li:last-child
返回最后一个li
内的ul
还没有尝试过,但我想这会起作用。
If you want the last link in the last ul (assuming the link is inside a
li
-element), this is what you want:#menu
returns the menu element.ul:last-child
returns the lastul
within#menu
li:last-child
returns the lastli
within thatul
Haven't tried it, but i guess this would work.
我只是猜测你的 HTML 是什么,但是:
I'm only guessing what your HTML is, but:
我已经在 safari (4.0.3) 和 firefox(3.5.4) 中测试了你的代码,一切正常,我不知道在 IE8 中!
考虑一下,这是 CSS3 标准中定义的新伪类(选择器),并非所有浏览器都完全支持它,而且最古老的浏览器也不支持!
作为一般规则,出于性能原因,我建议您避免使用超过 2 级的 CSS 指令(如果您确实需要,最多 3 级)!
然而,我认为如果你不想将样式直接应用到标记元素,你必须找到一个javascript解决方案!
I've tested your code in safari (4.0.3) and firefox(3.5.4) and all works fine, I don't know in IE8!
Consider that this is a new pseudo-class (selector) defined in the CSS3 standard that is not completely supported from all browsers and is unsupported by the oldest browsers!
As general rule, for performance reasons, I suggest you to avoid CSS directives with more than 2 level (maximum 3 if you really need it)!
However, I think that if you don't want to apply the style directly to a tagged element, you have to find a javascript solution!
CSS
last-child
在 IE 中不起作用,包括 IE8。它应该适用于所有其他当前浏览器,但如果您计划在需要支持 IE 的网站上使用它,则需要找到一种替代方法来实现。
此页面有 CSS 选择器的完整列表,以及支持它们的浏览器:
http://quirksmode.org/css/contents.html
CSS
last-child
does not work in IE, including IE8.It should work in all other current browsers, but if you're planning to use it on a site which needs to support IE, you'll need to work out an alternative way to do it.
This page has a full list of CSS selectors, and which browsers support them:
http://quirksmode.org/css/contents.html