javascript - CSS 第 n 次选择

标签 javascript html css css-selectors

我有 4 个段落 <p> .我需要选择除保留此结构上的前两段之外的所有段落:

<div class="right">
  <a></a>
  <p>heading</p>
  <p></p>
  <p style="margin-bottom:6.0pt;vertical-align:baseline;">
    <span style="color:#2F2F2F ;">hi....Each day the world meets your expectations. It's no secret that your perspective shapes your experience.
    What's remarkable, however, is how belief builds awareness; and how with every insight, doors open, new futures unfold 
    and once-only-dreamed-of possibilities become everyday realities.</span>
  </p>
  <p style="margin-bottom:6.0pt;vertical-align:baseline;">
    <span style="color:#2F2F2F ;">Each day the world meets your expectations. It's no secret that your perspective shapes your experience.
    What's remarkable, however, is how belief builds awareness; and how with every insight, doors open, new futures unfold 
    and once-only-dreamed-of possibilities become everyday realities.</span>
  </p>

  <p style="margin-bottom:6.0pt;vertical-align:baseline;">
    <span style="color:#2F2F2F ;">Each day the world meets your expectations. It's no secret that your perspective shapes your experience.
    What's remarkable, however, is how belief builds awareness; and how with every insight, doors open, new futures unfold 
    and once-only-dreamed-of possibilities become everyday realities.</span>
  </p>
</div>

我在下面写了css,

.right p:not(:nth-child(4)){
  background:yellow;
}

但它只选择第一、第三和第四。

但是我们只需要第三和第四,而不是第一和第二

如何实现?

最佳答案

你可以使用 :nth-of-type相反,试试这个:

.right p:nth-of-type(n+3) {
  background:yellow;
}
<div class="right">
  <a href="#">I'm a link</a>
  <p>heading</p>
  <p>empty p</p>
  <p style="margin-bottom:6.0pt;vertical-align:baseline;">
    <span style="color:#2F2F2F ;">hi....Each day the world meets your expectations. It's no secret that your perspective shapes your experience.
What's remarkable, however, is how belief builds awareness; and how with every insight, doors open, new futures unfold 
and once-only-dreamed-of possibilities become everyday realities.</span>
  </p>
  <p style="margin-bottom:6.0pt;vertical-align:baseline;">
    <span style="color:#2F2F2F ;">Each day the world meets your expectations. It's no secret that your perspective shapes your experience.
What's remarkable, however, is how belief builds awareness; and how with every insight, doors open, new futures unfold 
and once-only-dreamed-of possibilities become everyday realities.</span>
  </p>

  <p style="margin-bottom:6.0pt;vertical-align:baseline;">
    <span style="color:#2F2F2F ;">Each day the world meets your expectations. It's no secret that your perspective shapes your experience.
What's remarkable, however, is how belief builds awareness; and how with every insight, doors open, new futures unfold 
and once-only-dreamed-of possibilities become everyday realities.</span>
  </p>
</div>

关于javascript - CSS 第 n 次选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40845586/

相关文章:

css - 补偿 Safari 的边框半径绘图

css - 让 <div> 标签停留,没有移动

javascript - 按每个对象的多个字段分组

Javascript/Jquery - 使用 Javascript/Jquery 将脚本添加到 div?

javascript - 如何清除 Javascript 中的单选按钮?

jquery - 设置列表项的宽度以填充无序列表

javascript - 我有一个 Uncaught Reference Error 说一个函数没有定义,尽管它似乎被定义了

javascript - 更改事件不起作用

来自实时源的 HTML5 音频流

css - 从旋转的 div 旋转背景图像