為什么在if (index % 2),當其值為0是,是假?
if?(index?%?2)?{ ????????????????$(this).css('color','blue')?怎么理解?(其判斷規(guī)則,何時為真,何時又為假?)
$("button:last").click(function() {
? ? ? ? //遍歷所有的li
? ? ? ? //修改偶數li內的字體顏色
? ? ? ? $("li").each(function(index, element) {
? ? ? ? ? ? if (index % 2) {
? ? ? ? ? ? ? ? $(this).css('color','blue')
? ? ? ? ? ? }
? ? ? ? })
? ? })
? ? </script>
2016-09-09
你并不要在意余數為0 時就是假,不管它是不是余數,你要知道,在javascript中0代表false,非0代表true,哥們,javascript基礎要補補了。
2018-02-04
記住index % 2 余數為一就為真
2017-03-29
javascript規(guī)定就是這樣的啊
2016-09-09
6個 li 的下標是 index, 分別是0,1,2,3,4,5 ?從0開始計算下標...所以當index=0,2,4時,%為取余計算
index % 2 = 0,為假,不執(zhí)行藍色. ?所以當 index=1,3,5時index % 2 = 1,為真,執(zhí)行, ?
2016-09-09
0就是fasle啊,就是假啊。index除以2看余多少,余0就是假,否則就是真