简体   繁体   English

使用CSS(LESS)选择与所选元素(已附加唯一类)有关的N个下一个兄弟姐妹

[英]Select N next siblings in relation to the selected element (with unique class attached) using CSS (LESS)

I have HTML code below. 我下面有HTML代码。 Let's say, I'm able to attach the selected-day class by clicking on each span. 假设,我可以通过单击每个跨度来附加selected-day课程。 I'm trying to select the next 6 siblings, starting from the element with the class selected-day . 我试图选择接下来的6个兄弟姐妹,从具有selected-day类的元素开始。 Here's what I have came up with: 这是我想出的:

 #calendar span.day.selected-day { background-color: red; ~ span:nth-child(n + 6):nth-child(-n + 15) { background-color: red; } } 
 <div id="calendar"> <span class="day">1</span> <span class="day">2</span> <span class="day">3</span> <span class="day">4</span> <span class="day">5</span> <span class="day selected-day">6</span> <span class="day">7</span> <span class="day">8</span> <span class="day">9</span> <span class="day">10</span> <span class="day">11</span> <span class="day">12</span> <span class="day">13</span> <span class="day">14</span> <span class="day">15</span> <span class="day">16</span> </div> 

While it correctly highlights selected days at the beginning: 当它正确地突出显示开始时的选定日期时:

在此处输入图片说明

Whenever I change the selection, it doesn't follow: 每当我更改选择时,它都不会跟随:

在此处输入图片说明

Is there any proper way to always select the next 6 sibling in relation to the element with the given class? 有什么适当的方法可以始终选择与给定类的元素有关的下6个同级对象?

Well it doesn't look elegant but you can simply add multiple selectors. 好吧,它看起来并不优雅,但是您可以简单地添加多个选择器。 Since I don't use less I write the example in native css 由于我不减少使用,所以我在本地CSS中编写了示例

#calendar span.day.selected-day + span,
#calendar span.day.selected-day + span + span,
#calendar span.day.selected-day + span + span + span,
#calendar span.day.selected-day + span + span + span + span,
#calendar span.day.selected-day + span + span + span + span + span,
#calendar span.day.selected-day + span + span + span + span + span + span {
    background-color: red;
}

Edit: I tried it in LESS 编辑:我试过少

#calendar span.day.selected-day {
  background-color: red;
  + span,
  + span + span,
  + span + span + span,
  + span + span + span + span,
  + span + span + span + span + span,
  + span + span + span + span + span + span
  {
    background-color: red;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM