我试图从容器中制作成对或未对的div,以获得不同的背景。例如,第一个有红色背景,第二个蓝色,第三个红色,第四个蓝色,所以.
这是我的密码:
<div>...</div>
<div>...</div>
<div>...</div>
<div>...</div>
<div>...</div>
<div>...</div>
div { width:50px; height:50px; background:red; }
我真的不知道怎么做到这一点。我不知道我是否也只需要css或jquery。
谢谢。
发布于 2014-01-11 20:16:03
只需使用CSS就可以使用:nth-child
选择器来完成此操作:
div { width: 50px; height: 50px; }
div:nth-child(odd) { background-color: red; }
div:nth-child(even) { background-color: blue; }
发布于 2014-01-11 23:18:10
使用CSS属性的伪选择器nth-子。利用这个u可以选择奇偶节点。
div:nth-child(odd) { background-color: #FF0000; }
div:nth-child(even) { background-color: #0000FF; }
https://stackoverflow.com/questions/21070892
复制相似问题