简体   繁体   English

CSS / JS:在两个父元素之间定位:nth-​​child一致性?

[英]CSS/JS: Targeting :nth-child consistant across 2 parent elements?

Let's say we have a setup like this: 假设我们有这样的设置:

 .col:nth-child(2n+1) { background: red; } /* Style the div's for testing */ div.row { width: 100%; border: 1px solid blue; } div.col { padding: 12px 0; width: 100%; } 
 <div class="row"><!-- The .col's in this row are dynamically generated --> <div class="col 1"></div> <div class="col 2"></div> <div class="col 3"></div> <div class="col 4"></div> <div class="col 5"></div> </div> <div class="row"> <div class="col 1"></div> <div class="col 2"></div> <div class="col 3"></div> <div class="col 4"></div> </div> 

Here's a working Fiddle: https://jsfiddle.net/1L4rodh4/ 这是一个工作中的小提琴: https : //jsfiddle.net/1L4rodh4/

Now... the Nth selector is working as it should. 现在...第Nth selector正在正常工作。 The problem is that I want to get the elements styled consistently. 问题是我想要使样式一致。 so that it appears to be seamless between the 2 .row 's. 因此它似乎在2个.row之间是无缝的。 The .col 's in the first .row are dynamically generated and could end up being even or odd . 第一个.row中的.col是动态生成的,最终可能是evenodd

Please ask me for clarification if I wasn't clear. 请问我是否不清楚。 I couldn't find any information about this elsewhere. 我在其他地方找不到有关此的任何信息。 This is something that seems so simple, yet it seems to be impossible. 这看起来很简单,但似乎是不可能的。

Is there a better way of doing this, or am I stuck? 有没有更好的方法可以做到这一点,还是我被困住了?


I understand I could use JS to get the number of "col"'s in the first "row" and change the selector in the second row. 我知道我可以使用JS来获取第一行中“ col”的数量,并更改第二行中的选择器。 That just seems way too hack-ish for this though. 但这似乎太过分了。

EDIT: I will accept a JS solution. 编辑:我将接受一个JS解决方案。


The following is a working example of how this can be done with a simple plain JS script, based on the code in the selected answer below. 以下是一个工作示例,基于下面所选答案中的代码,可以使用简单的普通JS脚本完成此操作。

 var cols = document.querySelectorAll('.table.js-fix .tr'); for (var i = 0; i < cols.length; i++) { cols[i].classList.add(i % 2 == 0 ? 'even' : 'odd'); } 
 .table.js-fix .tr:nth-child(2n + 1), /* :nth-child fallback */ .table.js-fix .tr.even { background: red; } .table.js-fix .tr.odd { background: #fff; } /* Style for testing */ .table { width: 100%; border: 1px solid blue; } .tr { padding: 12px 0; width: 100%; } 
 <div class="row"><!-- The .tr in this row is just used as a thead --> <div class="col"> <div class="table"> <div class="tr">Section Title</div> </div> </div> </div> <div class="row"><!-- The .tr's in this row are dynamically generated --> <div class="col"> <div class="table js-fix"> <div class="tr"></div> <div class="tr"></div> <div class="tr"></div> </div> </div> </div> <div class="row"><!-- There will always be 5 .tr's in this row --> <div class="col"> <div class="table js-fix"> <div class="tr"></div> <div class="tr"></div> <div class="tr"></div> <div class="tr"></div> <div class="tr"></div> </div> </div> </div> 

JSFiddle with the above example: https://jsfiddle.net/n12h70j6/ JSFiddle与上面的示例: https ://jsfiddle.net/n12h70j6/

You can do that with CSS. 您可以使用CSS做到这一点。 See this or this . 看到这个这个

.col:first-child:nth-last-child(even) ~ .col:nth-child(2n + 1),
.col:first-child:nth-last-child(even){
  background-color: red;
}

.col:first-child:nth-last-child(odd) ~ .col:nth-child(2n + 2){
  background-color: red;
}

This checks if the number of children are even or odd and styles them appropriately. 这将检查孩子的数量是偶数还是奇数,并为它们适当地设置样式。

Update : Inspite of what it might look like. 更新 :尽管可能看起来像。 This will not work for cases when a parent with odd number of children directly follows another parent with odd number of children (which was the whole point). 对于具有奇数个孩子的父母直接跟随另一个具有奇数个孩子的父母(这是重点)的情况,这将不起作用。 However I am leaving this here for someone else to get ideas running in their heads. 但是,我将其留在这里,以便其他人将想法付诸实践。

Since it is still an answer, CSS alone cannot do this, so one must resort to javascript / jQuery. 由于仍然是一个答案,仅CSS不能做到这一点,因此必须使用javascript / jQuery。

This could be one way to do this with plain JS 这可能是使用普通JS做到这一点的一种方法

var cols = document.querySelectorAll('.col');
for(var i = 0; i < cols.length; i++)
    cols[i].classList.add(i % 2 == 0 ? 'some-class' : 'alternate-class');

No, there is no CSS-only way of doing this. 不,没有仅CSS方式可以做到这一点。

The reason is that CSS rules can only be qualified based on directly preceding children, or ancestors. 原因是CSS规则只能基于直接在前的子代或祖先来限定。 In you case, you want to qualify the second group based on what is inside the previous sibling (the first <div> group). 在你的情况下,您要根据什么是前一个兄弟(第一内侧晋级第二组<div>组)。

The only way to handle this is by means of something like adding a class to the first <div> that indicates whether it has an even or odd number of children. 解决此问题的唯一方法是通过将一个类添加到第一个<div>来指示其子级数是偶数还是奇数。

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

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