选择其他父母的孩子

时间:2013-08-09 00:27:14

标签: jquery html css

我有一个像这样的HTML模板:

<div id = "container">

   <div class = "status">
      <div class ="dynamic"></div>
   </div>

   <div class = "status">
      <div class ="dynamic"></div>
   </div>

   <div class = "status">
      <div class ="dynamic"></div>
   </div>

   <div class = "status">
      <div class ="dynamic"></div>
   </div>

</div>

是否可以使用CSS或jQuery更改其他.status的{​​{1}}的背景?

3 个答案:

答案 0 :(得分:2)

您可以在纯CSS中执行此操作:

#container > .status:nth-child(2n+1) > .dynamic {
    /* Change the background */
}

(Here’s a demo!)

答案 1 :(得分:1)

这可以通过CSS完成:

#container > .status:nth-child(2n) > .dynamic { background: whatever-you-want; }

#container > .status:nth-child(2n+1) > .dynamic { background: whatever-you-want; }

答案 2 :(得分:0)

尝试

$('.status:odd .dynamic').css('color', 'blue')

$('.dynamic:even').css('color', 'blue')

演示:fiddle