each問(wèn)題
? if (index % 2) {
? ? ? ? ? ? ? ? $(this).css('color','blue')
? ? ? ? ? ? }
這樣不是給奇數(shù)添加樣式么??? 如果這樣的話??? $('li:even').css('color','blue') 才是對(duì)的么?
? if (index % 2) {
? ? ? ? ? ? ? ? $(this).css('color','blue')
? ? ? ? ? ? }
這樣不是給奇數(shù)添加樣式么??? 如果這樣的話??? $('li:even').css('color','blue') 才是對(duì)的么?
2016-12-25
舉報(bào)
2018-10-19
% 為取模運(yùn)算符,if( index % 2) ,當(dāng)index為偶數(shù)時(shí),index % 2余數(shù)為 0,結(jié)果為 false
當(dāng)index為奇數(shù)時(shí),index % 2 != 0 結(jié)果為 true,執(zhí)行下面代碼
那么如何讓 index 為偶數(shù)時(shí) 執(zhí)行 index % 2呢,就像樓上說(shuō)的,只需要把判斷條件改為 index % 2 == 0
2017-01-12
? if (index % 2) {
? ? ? ? ? ? ? ? $(this).css('color','blue')
? ? ? ? ? ? }
這是給偶數(shù)加的哦。index 算出來(lái)的值,為1 3 5.而li的下標(biāo)是從0開(kāi)始算了,所以在dom結(jié)構(gòu)上是給偶數(shù)的li加了顏色。
? $('li:even').css('color','blue') ? ?:even屬于css選擇器,是從一開(kāi)始數(shù)的。
所以?xún)蓚€(gè)并不一樣哦。
2016-12-25
他是系數(shù)從0開(kāi)始的
2016-12-25
都是對(duì)的,多個(gè)方法達(dá)到一個(gè)目的