This article analyzes the method of jQuery to achieve interlaced color change. Share it with everyone for your reference, the details are as follows:
Native js:
var tab = document.getElementByTagName('table')[0]; var tr = tab.getElementByTagName('tr'); for(var i=0;i<tr.length;i++){ if(i%2==0){ tr[i].style.background="orange"; }else{ tr[i].style.background="#abcdef"; } }
jquery dom selector:
$('table tr:odd').css('background','orange'); $('table tr:even').css('background','#abcdef');
jquery method selector:
$('table tr').filter(':odd').css('background','orange').end().filter(':even').css('background','#abcdef');
There are many methods, learn more Knowledge, one less line of code to write.