简体   繁体   English

如何定位元素首次出现的第一个孩子

[英]how do I target the first child of the first appearance of an element

Ok so I have several blocks of code that look like: 好的,所以我有几个看起来像这样的代码块:

    <div class="news-sec">
        <h3 class="col-xs-8">heading</h3>
        <p class="col-xs-4>paragraph</p>
    </div>
  <div class="news-sec">
        <h3 class="col-xs-8">heading</h3>
        <p class="col-xs-4>paragraph</p>
    </div>
  <div class="news-sec">
        <h3 class="col-xs-8">heading</h3>
        <p class="col-xs-4>paragraph</p>
    </div>

My trouble is that the class is being output by drupal. 我的麻烦是该类由drupal输出。 I need the first one not to have the bootstrap classes, however because they are all the same field they all get spit out wrapped up in the col-xs-# divs. 我需要第一个没有bootstrap类,但是因为它们都是相同的字段,所以它们都被打包在col-xs-# div中。 I figure I can write some css to negate the effects of bootstrap such as: 我想我可以写一些CSS来抵消引导的影响,例如:

.news-sec h3:first-of-type{
    width:100%;
    float:none;
}

What I've learned though is that this will affect every .news-sec top level h3 instead of looking at only the first .news-sec . 但是我了解到,这将影响每个.news-sec顶级h3而不是仅关注第一个.news-sec Is there a way to accomplish what I want to do without js. 有没有一种方法可以在没有js的情况下完成我想做的事情。

PS My drupal dev is out for the week and I can't edit the way it's being output, only after. PS我的drupal开发人员已经用了一周的时间,只有在之后,我才能编辑其输出方式。

.news-sec h3:first-child定位第一个,并且仅第一个。

.news-sec:first-child h3 is what you want. .news-sec:first-child h3是您想要的。 This gets the first .news-sec and applies the style to its h3 . 这将获取第一个.news-sec并将样式应用于其h3 Note how I've applied the pseudo selector ( :first-child ) to the .news-sec instead of the h3 注意我如何将伪选择器( :first-child )应用于.news-sec而不是h3

(Compare this to your code: .news-sec h3:first-of-type -- Your code was looking for the first h3 inside of all .news-sec ) (将此与您的代码进行比较: .news-sec h3:first-of-type您的代码正在所有.news-sec寻找第一个h3

Here's a demo: 这是一个演示:

 .news-sec:first-child h3 { color: red } 
 <div class="news-sec"> <h3 class="col-xs-8">heading</h3> <p class="col-xs-4">paragraph</p> </div> <div class=" news-sec "> <h3 class="col-xs-8">heading</h3> <p class="col-xs-4">paragraph</p> </div> <div class="news-sec"> <h3 class="col-xs-8">heading</h3> <p class="col-xs-4">paragraph</p> </div> 

Side note: You're missing some quotes in your provided HTML code near each col-xs-4 . 旁注:您在每个col-xs-4附近的HTML代码中缺少一些引号。

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

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