我有一个由JS框架生成的DOM struct ,可能如下所示:
span:nth-child(odd) { background-color: lightblue; }
span {
display: block;
}
<div id="parent">
<div class="child">
<span>Some text to color in alternate colors with the previous span no matter if it's the same parent or not</span>
<span>Some text to color in alternate colors with the previous span no matter if it's the same parent or not</span>
</div>
<div class="child">
<span>Some text to color in alternate colors with the previous span no matter if it's the same parent or not</span>
</div>
<div class="child">
<span>Some text to color in alternate colors with the previous span no matter if it's the same parent or not</span>
<span>Some text to color in alternate colors with the previous span no matter if it's the same parent or not</span>
<span>Some text to color in alternate colors with the previous span no matter if it's the same parent or not</span>
</div>
<div class="child">
<span>Some text to color in alternate colors with the previous span no matter if it's the same parent or not</span>
<span>Some text to color in alternate colors with the previous span no matter if it's the same parent or not</span>
<span>Some text to color in alternate colors with the previous span no matter if it's the same parent or not</span>
<span>Some text to color in alternate colors with the previous span no matter if it's the same parent or not</span>
<span>Some text to color in alternate colors with the previous span no matter if it's the same parent or not</span>
<span>Some text to color in alternate colors with the previous span no matter if it's the same parent or not</span>
</div>
<div class="child">
<span>Some text to color in alternate colors</span>
</div>
</div>
无论父对象是否相同,我都会try 对跨度进行不同的着色. 我用伪 Select 器和这个问题CSS div alternating colour试了试,但没有成功.这是否只能通过CSS来实现,或者我应该深入研究基于JS框架的解决方案?