2010-10-21 28 views
0
<div id="autoSuggestedPlaces"> 

     <div> 
      <div> 

      </div> 
     </div> 

     <div> 
      <div > 

      </div> 
     </div> 

     <div> 
      <div> 

      </div> 
     </div> 


</div> 

我的HTML標記lookes這樣我想補充,我想補充甚至DIV有不同勢bacground顏色和奇數格有不同勢顏色,我試過,但採用div .even和div.odd但它不工作。要使用jQuery和CSS添加不同顏色

+3

爲什麼你有相同的ID 'addAPlaceSearchResult'? – 2010-10-21 06:14:25

+0

是你的例子4格與BG顏色或3?換句話說就是父母的div#1或者你只考慮孩子div? – 2010-10-21 06:24:30

回答

5

試試這個

$("div#autoSuggestedPlaces div div:even").css("background-color", "#bbbbff"); 
$("div#autoSuggestedPlaces div div:odd").css("background-color", "#ffffff"); 
+1

+1,儘管簡單地添加一個偶數/奇數類並在CSS中設置樣式可能更好。國際海事組織,這強制顯示功能分離*稍微*更好。 – 2010-10-21 11:00:57

+0

我是第二個鮑比傑克。 – 2010-11-15 08:54:02

0

嘗試使用:even:odd

div.even將選擇與類名even所有div元素。

正如@Haim所指出的,從不使用相同的ID來表示多個元素。

.first 
{ 
    background-color: yourfirstcolor; 
} 


.second 
{ 
    background-color: yoursecondcolor; 
} 

$("#autoSuggestedPlaces div:even").addClass("first"); 
$("#autoSuggestedPlaces div:odd").addClass("second"); 
0

的代碼是這樣的:

$("div:odd").css("background-color", "#F4F4F8"); 
    $("div:even").css("background-color", "#EFF1F1"); 

和在同一文檔中使用相同的ID兩次無效HTML。修復你的HTML。