JS Alternate Color Effect code

Source: Internet
Author: User
The code is as follows Copy Code

This function is need to work around
A bug in IE related to element attributes
function Hasclass (obj) {
var result = false;
if (Obj.getattributenode ("class")!= null) {
result = Obj.getattributenode ("class"). Value;
}
return result;
}

function stripe (ID) {

The flag we ' ll use to keep track of
Whether the current row is odd or even
var even = false;

If arguments are provided to specify the colours
Of the even & odd rows, then use the them;
Otherwise use the following defaults:
var evencolor = arguments[1]? ARGUMENTS[1]: "#fff";
var oddcolor = arguments[2]? ARGUMENTS[2]: "#eee";

Obtain a reference to the desired table
If no such table exists, abort
var table = document.getElementById (ID);
if (! table) {return;}

By definition, tables can have more than one tbody
element, so we'll have to get the list of child
<tbody>s
var tbodies = table.getelementsbytagname ("tbody");

and iterate through them ...
for (var h = 0; h < tbodies.length; h++) {

Find All &lt;tr&gt; Elements ...
var trs = Tbodies[h].getelementsbytagname ("tr");

... and iterate through them
for (var i = 0; i < trs.length; i++) {

Avoid rows that have a class attribute
or BackgroundColor style
if (!hasclass (trs[i]) &&! Trs[i].style.backgroundcolor) {

Get all the cells in this row ...
var tds = Trs[i].getelementsbytagname ("TD");

and iterate through them ...
for (var j = 0; J < Tds.length; J + +) {

var mytd = tds[j];

Avoid cells that have a class attribute
or BackgroundColor style
if (! hasclass (MYTD) &&! Mytd.style.backgroundColor) {

Mytd.style.backgroundColor = even? Evencolor:oddcolor;

}
}
}
Flip from odd to even, or vice-versa
even =! even;
}
}
}


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.