each問題
? if (index % 2) {
? ? ? ? ? ? ? ? $(this).css('color','blue')
? ? ? ? ? ? }
這樣不是給奇數添加樣式么??? 如果這樣的話??? $('li:even').css('color','blue') 才是對的么?
? if (index % 2) {
? ? ? ? ? ? ? ? $(this).css('color','blue')
? ? ? ? ? ? }
這樣不是給奇數添加樣式么??? 如果這樣的話??? $('li:even').css('color','blue') 才是對的么?
2016-12-25
舉報
2018-10-19
% 為取模運算符,if( index % 2) ,當index為偶數時,index % 2余數為 0,結果為 false
當index為奇數時,index % 2 != 0 結果為 true,執行下面代碼
那么如何讓 index 為偶數時 執行 index % 2呢,就像樓上說的,只需要把判斷條件改為 index % 2 == 0
2017-01-12
? if (index % 2) {
? ? ? ? ? ? ? ? $(this).css('color','blue')
? ? ? ? ? ? }
這是給偶數加的哦。index 算出來的值,為1 3 5.而li的下標是從0開始算了,所以在dom結構上是給偶數的li加了顏色。
? $('li:even').css('color','blue') ? ?:even屬于css選擇器,是從一開始數的。
所以兩個并不一樣哦。
2016-12-25
他是系數從0開始的
2016-12-25
都是對的,多個方法達到一個目的