CSS:怎么说 .class:last-of-type [类,而不是元素!]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13211453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CSS: How to say .class:last-of-type [classes, not elements!]
提问by Sliq
Possible Duplicate:
How do I select the “last child” with a specific class name in CSS?
As the title says, i want to adress css rules to the last element of a certain type which has a certain class. The following code works perfectly:
正如标题所说,我想将 css 规则寻址到具有特定类的特定类型的最后一个元素。以下代码完美运行:
div:last-of-type { margin-right:0; }
but i want something like
但我想要类似的东西
div.class:last-of-type { margin-right:0; }
How to do this ?
这该怎么做 ?
采纳答案by amustill
Unfortunately finding the last .class
is not possible with last-of-type
.
不幸的.class
是,使用last-of-type
.
Edit:
To actually add something constructive to this answer, you could fallback to JavaScript to locate this selector, or revise your markup/CSS. Whilst I have found myself in the situation that last-of-type
for a class selector would be useful, it's nothing that cannot be worked around with a little more thought.
编辑:要实际为此答案添加一些有建设性的内容,您可以回退到 JavaScript 以定位此选择器,或修改您的标记/CSS。虽然我发现自己处于last-of-type
一个类选择器会很有用的情况,但没有什么是不能多加思考的。
回答by NoBugs
Make the nomargin a css class, and you can change it easily:
将 nomargin 设为 css 类,您可以轻松更改它:
var items = document.getElementsByClassName('nomargin');
items[items.length-1].style.color = 'black';
items[items.length-1].style.background = 'white';
or if that doesn't have enough jQuery, you can use:
或者如果没有足够的 jQuery,您可以使用:
jQuery('div.nomargin:last').css({
color:'black',
background:'white'})