1. If you use JQuery, you can directly use JQuery.
$("tr:odd").addClass("clazzName");
$("tr:even").addClass("clazzName");
2. If you use pure js
1. Get the table tag first, var table = ()
2. Get the tbody tag var tbody = ("tbody")[0]
3. Get the tr tag var trs = ("tr")
4. Then iterate over trs
for(var i=0; i
if(i%2==0){
trs[i].="red";
}else{
trs[i].="blue";
}
}
It is necessary to obtain the tbody tag. Although you didn't write it, the browser will automatically add it when compiling the table.
All tr tags are in tbody
Copy the codeThe code is as follows:
$("tr:odd").addClass("clazzName");
$("tr:even").addClass("clazzName");
2. If you use pure js
1. Get the table tag first, var table = ()
2. Get the tbody tag var tbody = ("tbody")[0]
3. Get the tr tag var trs = ("tr")
4. Then iterate over trs
Copy the codeThe code is as follows:
for(var i=0; i
if(i%2==0){
trs[i].="red";
}else{
trs[i].="blue";
}
}
It is necessary to obtain the tbody tag. Although you didn't write it, the browser will automatically add it when compiling the table.
All tr tags are in tbody