Simple Method for controlling the style of odd and even rows in javascript forms _ javascript skills

Source: Internet
Author: User
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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.