:last-child 没有按预期工作?

新手上路,请多包涵

问题在于这个 CSS 和 HTML。这是带有示例代码 的 jsFiddle 的链接

HTML

 <ul>
    <li class"complete">1</li>
    <li class"complete">2</li>
    <li>3</li>
    <li>4</li>
</ul>

CSS

 li.complete:last-child {
    background-color:yellow;
}

li.complete:last-of-type {
    background-color:yellow;
}

这些 CSS 行中的任何一行都不应该 以“完整”类的最后一个 li 元素为 目标吗?

jQuery 中的这个查询也不针对它:

 $("li.complete:last-child");

但是这个可以:

 $("li.complete").last();

 li {
  background-color: green;
}
li.complete:first-child {
  background-color: white;
}
li.complete:first-of-type {
  background-color: red;
}
li.complete:last-of-type {
  background-color: blue;
}
li.complete:last-child {
  background-color: yellow;
}
 <ul>
  <li class="complete">1</li>
  <li class="complete">2</li>
  <li>3</li>
  <li>4</li>
</ul>

原文由 Nej Kutcharian 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 401
2 个回答

last-child 选择器用于选择父元素的最后一个子元素。它不能用于选择给定父元素下具有特定类的最后一个子元素。

复合选择器的另一部分(附加在 :last-child 之前)指定了最后一个子元素必须满足的额外条件才能被选中。在下面的代码片段中,您将看到所选元素如何根据复合选择器的其余部分而有所不同。

 .parent :last-child{ /* this will select all elements which are last child of .parent */
  font-weight: bold;
}

.parent div:last-child{ /* this will select the last child of .parent only if it is a div*/
  background: crimson;
}

.parent div.child-2:last-child{ /* this will select the last child of .parent only if it is a div and has the class child-2*/
  color: beige;
}
 <div class='parent'>
  <div class='child'>Child</div>
  <div class='child'>Child</div>
  <div class='child'>Child</div>
  <div>Child w/o class</div>
</div>
<div class='parent'>
  <div class='child'>Child</div>
  <div class='child'>Child</div>
  <div class='child'>Child</div>
  <div class='child-2'>Child w/o class</div>
</div>
<div class='parent'>
  <div class='child'>Child</div>
  <div class='child'>Child</div>
  <div class='child'>Child</div>
  <p>Child w/o class</p>
</div>

为了回答您的问题,下面将设置最后一个子元素 li 背景颜色为红色的样式。

 li:last-child{
    background-color: red;
}

但是以下选择器不适用于您的标记,因为 last-child 没有 class='complete' 即使它是 li

 li.complete:last-child{
    background-color: green;
}

如果(且仅当)您的标记中的最后一个 li 也有 class='complete' 时,它才会起作用。


要在 评论 中解决您的查询:

@Harry我觉得很奇怪: .complete:last-of-type 不起作用,但是 .complete:first-of-type 确实起作用,无论它的位置如何,它都是父元素。谢谢你的帮助。

选择器 .complete:first-of-type 在小提琴中起作用,因为它(即带有 class='complete' 的元素)仍然是父元素中 li 类型的第一个元素。尝试添加 <li>0</li> 作为 ul 下的第一个元素,您会发现 first-of-type 也会失败。这是因为 first-of-typelast-of-type 选择器选择父项下每种类型的第一个/最后一个元素。

有关选择器如何工作的更多详细信息,请参阅 线程中 BoltClock 发布的答案。这是最全面的:)

原文由 Harry 发布,翻译遵循 CC BY-SA 3.0 许可协议

:last-child 如果元素不是 VERY LAST 元素,则将不起作用

我认为添加/强调 :last-child 如果元素不是容器中的最后一个元素将不起作用是至关重要的。无论出于何种原因,我花了几个小时才意识到这一点,即使 Harry 的回答非常彻底,我也无法从 “最后一个子选择器用于选择父级的最后一个子元素”中提取该信息。

假设这是我的选择器: a:last-child {}

这有效:

 <div>
    <a></a>
    <a>This will be selected</a>
</div>

这不会:

 <div>
    <a></a>
    <a>This will no longer be selected</a>
    <div>This is now the last child :'( </div>
</div>

这不是因为 a 元素不是其父元素中的最后一个元素。

这可能很明显,但它不适合我……


边栏:这似乎是一个荒谬的陷阱,但魔鬼总是在细节中。 :last-of-type 在大多数情况下可能满足您的需求(并且感觉很直观),但是 :last-child 绝对是有目的的。它提供了更大的特异性(仅针对实际上是父母中最后一个孩子的那些元素)。这取决于您的用例。

原文由 Govind Rai 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题