Jquery 如何在div内部为div替换样式(背景色)
如果我有这样的HTML,如何使用id=“container”交替(偶数和奇数)为div中的div替换样式(使用jquery的背景色)Jquery 如何在div内部为div替换样式(背景色),jquery,css,css-selectors,Jquery,Css,Css Selectors,如果我有这样的HTML,如何使用id=“container”交替(偶数和奇数)为div中的div替换样式(使用jquery的背景色) <div id="container"> <div></div> <div></div> <div></div> <div></div> ... </div> 但是如何应用div这样的东西呢?它的工作方式与div完全相
<div id="container">
<div></div>
<div></div>
<div></div>
<div></div>
...
</div>
但是如何应用div这样的东西呢?它的工作方式与div完全相同。使用上述结构,您可以通过以下方式获得相同的效果:
#container div:nth-child(even) {background: #CCC;}
#container div:nth-child(odd) {background: #FFF;}
#container div:nth-child(even) {background: #CCC;}
#container div:nth-child(odd) {background: #FFF;}
使用相同的技巧:
您是否尝试过:
div#container div:nth-child(even) {background: #CCC;}
div#container div:nth-child(odd) {background: #FFF;}
无论标签是什么,第n个子项都应该工作。
<div id="container">
<div>A</div>
<div>B</div>
<div>C</div>
<div>D</div>
</div>
div#container div:nth-child(even) {background: #FF00CC;}
div#container div:nth-child(odd) {background: #CC00FF;}
A.
B
C
D
div#container div:n子级(偶数){background:#FF00CC;}
div#container div:nth child(奇数){background:#CC00FF;}
请尝试此链接@pistacchio您是正确的,但他通常用于表格的CSS也不正确。为什么人们总是抱怨某些选择器/属性的浏览器支持较差,而显然包括OP在内的许多人都不关心?这是IE7的一个很好的解决方案!
div#container div:nth-child(even) {background: #CCC;}
div#container div:nth-child(odd) {background: #FFF;}
<div id="container">
<div>A</div>
<div>B</div>
<div>C</div>
<div>D</div>
</div>
div#container div:nth-child(even) {background: #FF00CC;}
div#container div:nth-child(odd) {background: #CC00FF;}