This article introduces a simple method for controlling the odd and even row style of JavaScript forms. If you have any need, please refer to section 1. If you use JQuery, you can directly use JQuery.
The Code is as follows:
$ ("Tr: odd"). addClass ("clazzName ");
$ ("Tr: even"). addClass ("clazzName ");
2. If pure js is used
1. Obtain the table label first, var table = document. getElementById ()
2. Obtain the tbody label var tbody = table. getElementsByTagName ("tbody") [0].
3. Obtain the tr tag var trs = tbody. getElementsByTagName ("tr ")
4. Then iterate trs
The Code is as follows:
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 haven't written it, the browser will automatically add
All tr tags are in the tbody.