1. If you use JQuery, you can directly copy the code of JQuery
. The code is as follows:
$( "tr:odd").addClass("clazzName");
$("tr:even").addClass("clazzName");
2. If you are using pure js
1. Get the table tag first, var table = document.getElementById()
2. Then get the tbody tag inside var tbody = table.getElementsByTagName("tbody")[0]
3. Get it again tr tag var trs = tbody.getElementsByTagName("tr")
4. Then iterate trs
for(var i=0; i
if(i%2==0){
trs[i].style.backgroundColor="red";
} else{
trs[i].style.backgroundColor="blue";
}
}
It is necessary to obtain the tbody tag. Although you did not write it, the browser When compiling the table,
will be automatically added. All tr tags are in the tbody.