html - CSS 选择器 : first/last-of-type and has another class

标签 html css css-selectors

<分区>

在我正在构建的简单寻呼机中,如果当前页面是第一页或最后一页,我需要将特定样式应用于当前页面。我不知道如何使用 :first-of-type/:last-of-type 选择器以及普通的类选择器。

   <div id="myPages">
      <div class="something-else"></div>
      <div class="page-number current-page">1</div>
      <div class="page-number">2</div>
      <div class="page-number">3</div>
      <div class="another-one"></div>
  </div>

我想要像 #myPages .page-number:first-of-type.current-page 这样的东西,但是因为 something-else 它不起作用和另一个 div。

在我的jsFiddle例如,我希望“1”是蓝色的。

感谢您的帮助。

[edit] 我已经更新了 fiddle ,我认为这是获得我的想法的最佳方式:)

最佳答案

更新:由于帖子作者更改了他们的问题,我更新了我的答案:

#explain {
  margin-bottom: 20px;
}

#myPages .something-else+.current-page {
  color: blue;
}

#myPages div:nth-last-child(2) {
  color: red;
}
<div id="explain">
  Please note:<br> - there will be only one 'page-number' with 'current-page' in my app<br> - I don't know in advance how many pages there will be (so nth-child cannot work)
</div>
<div id="myPages">
  <div class="something-else"></div>
  <div class="page-number current-page">1 - Should be blue because it is the first page-number <strong>and</strong> it has the current-page class</div>
  <div class="page-number">2</div>
  <div class="page-number current-page">3 - Nothing should happen</div>
  <div class="page-number current-page">4 - Should be red because it is the last page-number <strong>and</strong> it has the current-page class</div>
  <div class="another-one"></div>
</div>

关于html - CSS 选择器 : first/last-of-type and has another class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44391954/

上一篇:html - 选择表格内和 div 内的图像

下一篇:html - 空跨度将其他跨度向上移动

相关文章:

html - 如何使用 PNG 来遮盖图像的边缘?

html - 不寻常的表行大小

css - 当标签内的输入字段处于事件状态时如何保持标签处于事件状态?

html - 显示:内联 block 不适用于 div

html - 可调整大小的固定顶部导航菜单

css - LESS CSS 伪类可以将选择器的属性传递给函数吗?

css - 这段非常规CSS代码的含义是什么?

javascript - 如何使用angularjs构建交互式滚动登陆页面?

html - 水平形式内的形式组过度拉伸(stretch)了先前的形式组

html - 同一空间中可变大小的元素