【问题标题】::last-child not working as expected?:last-child 没有按预期工作?
【发布时间】:2013-09-30 11:32:34
【问题描述】:

问题在于这个 CSS 和 HTML。这是带有示例代码的link to 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>

【问题讨论】:

标签: html css css-selectors


【解决方案1】:

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

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

.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' 时,它会起作用。


comments 中解决您的问题:

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

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

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

【讨论】:

  • 这太愚蠢了,但我认为你是对的。希望他们将其添加到 CSS4 规范中。
  • @NejKutcharian:是的,我认为它已经提出了很长时间。
  • @Harry 我觉得很奇怪: .complete:last-of-type 不起作用,但 .complete:first-of-type 确实起作用,无论它的位置如何,它都是父元素。感谢您的帮助。
  • @NejKutcharian:好的。 .complete:first-of-type 在小提琴中起作用,因为它仍然是父级中 li 类型的第一个元素。尝试将&lt;li&gt;0&lt;/li&gt; 添加为li 下的第一个元素,您会发现first-of-type 也会失败。
【解决方案2】:

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

除了 Harry 的回答之外,我认为添加/强调如果元素不是容器中的 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 元素不是其父元素中的最后一个元素。

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

【讨论】:

  • 双人!此外,nth-last-child 的工作方式相同。改用最后一个类型。
  • 非常感谢您提供的这些信息,它帮助我弄清楚了为什么我的 CSS 无法正常工作。我相信坚持者会认为这是正确的,但我确实希望伪类是您正在修改的选择器的最后一个子级,并且不仅适用于整个节点列表的最后一个子级。
  • 这对我有帮助。我有三个标签和一个 @Html.ValidationMessageFor 作为输入,最后一个孩子是 ValidationMessage...
  • 这非常有用。我花了几个小时试图让我的 li:last-child 工作,但紧随其后的是 div。最后一种对我有用
  • 在为这个问题苦苦挣扎了一段时间后,我搜索了stackoverflow,我在其中看到了这个答案,该答案已经获得了橙色的赞成票。看来你不是第一次帮我了哈哈
【解决方案3】:

我遇到类似的情况。我希望最后一个.item 的背景在看起来像...的元素中是黄色的。

<div class="container">
  <div class="item">item 1</div>
  <div class="item">item 2</div>
  <div class="item">item 3</div>
  ...
  <div class="item">item x</div>
  <div class="other">I'm here for some reasons</div>
</div>

我使用nth-last-child(2)来实现它。

.item:nth-last-child(2) {
  background-color: yellow;
}

这对我来说很奇怪,因为 item 的 nth-last-child 假设是最后一个 item 的第二个,但它有效,我得到了我期望的结果。 我从CSS Trick 发现了这个有用的技巧

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-07
    • 2021-10-19
    相关资源
    最近更新 更多