如何选择具有给定类名的第一个、第二个或第三个元素?

发布于 2024-08-30 15:25:10 字数 1204 浏览 10 评论 0原文

如何在元素列表中选择特定元素?我有以下内容:

<div class="myclass">my text1</div>
<!-- some other code follows -->
<div>
    <p>stuff</p>
</div>
<div>
    <p>more stuff</p>
</div>
<p>
    <span>Hello World</span>
</p>
<div class="myclass">my text2</div>
<!-- some other code follows -->
<div>
    <p>stuff</p>
</div>
<p>
    <span>Hello World</span>
</p>
<input type=""/>
<div class="myclass">my text3</div>
<!-- some other code follows -->
<div>
    <p>stuff</p>
</div>
<footer>The end</footer>

我有 CSS 类 div.myclass {doing things} 显然适用于所有内容,但我也希望能够选择类的第一个、第二个或第三个 div .myclass 像这样,无论它们在标记中的位置

div.myclass:first {color:#000;} 
div.myclass:second {color:#FFF;} 
div.myclass:third {color:#006;}

几乎像 jQuery 索引选择 .eq( index ),这就是我目前正在使用,但我需要一个无脚本的替代方案。

具体来说,我正在寻找伪选择器,而不是添加另一个类或使用 ID 来使事情正常工作之类的东西。

How can I select a certain element in a list of elements? I have the following:

<div class="myclass">my text1</div>
<!-- some other code follows -->
<div>
    <p>stuff</p>
</div>
<div>
    <p>more stuff</p>
</div>
<p>
    <span>Hello World</span>
</p>
<div class="myclass">my text2</div>
<!-- some other code follows -->
<div>
    <p>stuff</p>
</div>
<p>
    <span>Hello World</span>
</p>
<input type=""/>
<div class="myclass">my text3</div>
<!-- some other code follows -->
<div>
    <p>stuff</p>
</div>
<footer>The end</footer>

I have the CSS class div.myclass {doing things} that applies to all, obviously, but I also wanted to be able to select the first, second, or third div of class .myclass like this, regardless of where they are in the markup:

div.myclass:first {color:#000;} 
div.myclass:second {color:#FFF;} 
div.myclass:third {color:#006;}

Almost like the jQuery index selection .eq( index ), which is what I am using currently, but I need a no-script alternative.

To be specific, I am looking for pseudo selectors, not things like adding another class or using IDs to make things work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

默嘫て 2024-09-06 15:25:10

使用第 n 个子项(项目编号)
EX

<div class="parent_class">
    <div class="myclass">my text1</div>
    some other code+containers...

    <div class="myclass">my text2</div>
    some other code+containers...

    <div class="myclass">my text3</div>
    some other code+containers...
</div>
.parent_class:nth-child(1) { };
.parent_class:nth-child(2) { };
.parent_class:nth-child(3) { };

OR

:第 n 个类型(项目编号)
与你的代码相同

.myclass:nth-of-type(1) { };
.myclass:nth-of-type(2) { };
.myclass:nth-of-type(3) { };

use nth-child(item number)
EX

<div class="parent_class">
    <div class="myclass">my text1</div>
    some other code+containers...

    <div class="myclass">my text2</div>
    some other code+containers...

    <div class="myclass">my text3</div>
    some other code+containers...
</div>
.parent_class:nth-child(1) { };
.parent_class:nth-child(2) { };
.parent_class:nth-child(3) { };

OR

:nth-of-type(item number)
same your code

.myclass:nth-of-type(1) { };
.myclass:nth-of-type(2) { };
.myclass:nth-of-type(3) { };
折戟 2024-09-06 15:25:10

从发布这个问题到今天,您可能终于意识到了这一点,但是选择器的本质使得不可能在分层不相关的 HTML 元素中导航。

或者,简单地说,既然您在评论中说

没有统一的父容器

...如果不以其他答案所示的某种方式修改标记,单独使用选择器是不可能的。

必须使用 jQuery .eq() 解决方案。

You probably finally realized this between posting this question and today, but the very nature of selectors makes it impossible to navigate through hierarchically unrelated HTML elements.

Or, to put it simply, since you said in your comment that

there are no uniform parent containers

... it's just not possible with selectors alone, without modifying the markup in some way as shown by the other answers.

You have to use the jQuery .eq() solution.

恏ㄋ傷疤忘ㄋ疼 2024-09-06 15:25:10

这与其说是一个答案,不如说是一个非答案,即一个示例,说明为什么上面得到高度评价的答案之一实际上是错误的。

我觉得这个答案看起来不错。事实上,它给了我我正在寻找的东西::nth-of-type,这对于我的情况来说是有效的。 (所以,谢谢@Bdwey。)

我最初阅读了@BoltClock 的评论(它说答案本质上是错误的)并驳回了它,因为我已经检查了我的用例,并且它有效。然后我意识到 @BoltClock 拥有 300,000+(!) 的声誉,并且有一个自称是 CSS 大师的个人资料。嗯,我想,也许我应该再仔细看看。

结果如下: div.myclass:nth-of-type(2) 并不意味着“div.myclass 的第二个实例”。相反,它的意思是“div 的第二个实例,并且它还必须具有 'myclass' 类”。当 div.myclass 实例之间存在中间的 div 时,这是一个重要的区别。

我花了一些时间才弄清楚这个问题。因此,为了帮助其他人更快地理解它,我编写了一个示例,我相信它比书面描述更清楚地演示了这个概念:我劫持了 h1h2、h3h4 元素本质上是 div。我在其中一些上放置了一个 A 类,将它们分组为 3,然后使用 h?.A:nth- 将第一个、第二个和第三个实例着色为蓝色、橙色和绿色of-type(?)。 (但是,如果你仔细阅读,你应该问“第一个、第二个和第三个实例是什么?”)。我还在某些组中插入了不同的(即不同的 h 级别)或相似的(即相同的 h 级别)未分类元素。

请特别注意最后一组 3。此处,未分类的 h3 元素插入在第一个和第二个 h3.A 元素之间。在这种情况下,不会出现第二种颜色(即橙色),而第三种颜色(即绿色)会出现在 h3.A 的第二个实例上。这表明 h3.A:nth-of-type(n) 中的 n 计算的是 h3,而不是 h3.As。

嗯,希望有帮助。谢谢,@BoltClock。

div {
  margin-bottom: 2em;
  border: red solid 1px;
  background-color: lightyellow;
}

h1,
h2,
h3,
h4 {
  font-size: 12pt;
  margin: 5px;
}

h1.A:nth-of-type(1),
h2.A:nth-of-type(1),
h3.A:nth-of-type(1) {
  background-color: cyan;
}

h1.A:nth-of-type(2),
h2.A:nth-of-type(2),
h3.A:nth-of-type(2) {
  background-color: orange;
}

h1.A:nth-of-type(3),
h2.A:nth-of-type(3),
h3.A:nth-of-type(3) {
  background-color: lightgreen;
}
<div>
  <h1 class="A">h1.A #1</h1>
  <h1 class="A">h1.A #2</h1>
  <h1 class="A">h1.A #3</h1>
</div>

<div>
  <h2 class="A">h2.A #1</h2>
  <h4>this intervening element is a different type, i.e. h4 not h2</h4>
  <h2 class="A">h2.A #2</h2>
  <h2 class="A">h2.A #3</h2>
</div>

<div>
  <h3 class="A">h3.A #1</h3>
  <h3>this intervening element is the same type, i.e. h3, but has no class</h3>
  <h3 class="A">h3.A #2</h3>
  <h3 class="A">h3.A #3</h3>
</div>

This isn't so much an answer as a non-answer, i.e. an example showing why one of the highly voted answers above is actually wrong.

I thought that answer looked good. In fact, it gave me what I was looking for: :nth-of-type which, for my situation, worked. (So, thanks for that, @Bdwey.)

I initially read the comment by @BoltClock (which says that the answer is essentially wrong) and dismissed it, as I had checked my use case, and it worked. Then I realized @BoltClock had a reputation of 300,000+(!) and has a profile where he claims to be a CSS guru. Hmm, I thought, maybe I should look a little closer.

Turns out as follows: div.myclass:nth-of-type(2) does NOT mean "the 2nd instance of div.myclass". Rather, it means "the 2nd instance of div, and it must also have the 'myclass' class". That's an important distinction when there are intervening divs between your div.myclass instances.

It took me some time to get my head around this. So, to help others figure it out more quickly, I've written an example which I believe demonstrates the concept more clearly than a written description: I've hijacked the h1, h2, h3 and h4 elements to essentially be divs. I've put an A class on some of them, grouped them in 3's, and then colored the 1st, 2nd and 3rd instances blue, orange and green using h?.A:nth-of-type(?). (But, if you're reading carefully, you should be asking "the 1st, 2nd and 3rd instances of what?"). I also interjected a dissimilar (i.e. different h level) or similar (i.e. same h level) un-classed element into some of the groups.

Note, in particular, the last grouping of 3. Here, an un-classed h3 element is inserted between the first and second h3.A elements. In this case, no 2nd color (i.e. orange) appears, and the 3rd color (i.e. green) shows up on the 2nd instance of h3.A. This shows that the n in h3.A:nth-of-type(n) is counting the h3s, not the h3.As.

Well, hope that helps. And thanks, @BoltClock.

div {
  margin-bottom: 2em;
  border: red solid 1px;
  background-color: lightyellow;
}

h1,
h2,
h3,
h4 {
  font-size: 12pt;
  margin: 5px;
}

h1.A:nth-of-type(1),
h2.A:nth-of-type(1),
h3.A:nth-of-type(1) {
  background-color: cyan;
}

h1.A:nth-of-type(2),
h2.A:nth-of-type(2),
h3.A:nth-of-type(2) {
  background-color: orange;
}

h1.A:nth-of-type(3),
h2.A:nth-of-type(3),
h3.A:nth-of-type(3) {
  background-color: lightgreen;
}
<div>
  <h1 class="A">h1.A #1</h1>
  <h1 class="A">h1.A #2</h1>
  <h1 class="A">h1.A #3</h1>
</div>

<div>
  <h2 class="A">h2.A #1</h2>
  <h4>this intervening element is a different type, i.e. h4 not h2</h4>
  <h2 class="A">h2.A #2</h2>
  <h2 class="A">h2.A #3</h2>
</div>

<div>
  <h3 class="A">h3.A #1</h3>
  <h3>this intervening element is the same type, i.e. h3, but has no class</h3>
  <h3 class="A">h3.A #2</h3>
  <h3 class="A">h3.A #3</h3>
</div>

◇流星雨 2024-09-06 15:25:10

是的,你可以这样做。例如,要设置构成表格不同列的 td 标签的样式,您可以执行以下操作:

table.myClass tr > td:first-child /* First column */
{
  /* some style here */
}
table.myClass tr > td:first-child+td /* Second column */
{
  /* some style here */
}
table.myClass tr > td:first-child+td+td /* Third column */
{
  /* some style here */
}

Yes, you can do this. For example, to style the td tags that make up the different columns of a table you could do something like this:

table.myClass tr > td:first-child /* First column */
{
  /* some style here */
}
table.myClass tr > td:first-child+td /* Second column */
{
  /* some style here */
}
table.myClass tr > td:first-child+td+td /* Third column */
{
  /* some style here */
}
饮惑 2024-09-06 15:25:10

也许使用 CSS 的“~”选择器?

.myclass {
    background: red;
}

.myclass~.myclass {
    background: yellow;
}

.myclass~.myclass~.myclass {
    background: green;
}

请参阅我的示例 jsfiddle

Perhaps using the "~" selector of CSS?

.myclass {
    background: red;
}

.myclass~.myclass {
    background: yellow;
}

.myclass~.myclass~.myclass {
    background: green;
}

See my example on jsfiddle

恰似旧人归 2024-09-06 15:25:10

将来(也许)您将能够使用 :nth-child(an+b of s)

在您的情况下,它将是 :nth-child(-n+3 of . myclass)

实际上,浏览器对“of”过滤器的支持非常有限。似乎只有 Safari 支持该语法。

https://css-tricks.com/almanac/selectors/n/nth -子/

In the future (perhaps) you will be able to use :nth-child(an+b of s)

In your case it would be :nth-child(-n+3 of .myclass)

Actually, browser support for the “of” filter is very limited. Only Safari seems to support the syntax.

https://css-tricks.com/almanac/selectors/n/nth-child/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文