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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 20:41:41  来源:igfitidea点击:

CSS: How to say .class:last-of-type [classes, not elements!]

csscss-selectors

提问by Sliq

Possible Duplicate:
How do I select the “last child” with a specific class name in CSS?

可能的重复:
如何在 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 .classis 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-typefor 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'})