Skip to content

Instantly share code, notes, and snippets.

@titanew
Last active December 17, 2015 19:29
Show Gist options
  • Save titanew/5660477 to your computer and use it in GitHub Desktop.
Save titanew/5660477 to your computer and use it in GitHub Desktop.
表格斑马线
var zebra = function(klass) {
var table = document.getElementsByTagName("table");
var reg = new RegExp("(^|\s)" + klass + "(\s|$)");
// var reg = /(^|\s)main\-table(\s|$)/
for (var i = 0; i < table.length; i++) {
var cls = table[i].getAttribute("class");
if (reg.test(cls)) {
var tr = table[i].getElementsByTagName('tr');
for (var j = 1; j < tr.length; j++) {
if (j % 2 == 0) {
tr[j].style.background = "#f1f3f4";
};
};
table[i].onmouseover = function(e) {
var e = e || event;
var target = e.target || e.srcElement;
if (target.tagName.toLowerCase() == "td") {
var child = target.parentNode.childNodes;
for (var t = 0; t < child.length; t++) {
var arr = [];
if (child[t].nodeName.toLowerCase() == "td") {
arr.push(child[t]);
for (var e = 0; e < arr.length; e++) {
arr[e].style.background = "#333";
};
};
};
};
};
table[i].onmouseout = function(e) {
var e = e || event;
var target = e.target || e.srcElement;
if (target.tagName.toLowerCase() == "td") {
var child = target.parentNode.childNodes;
for (var j = 0; j < child.length; j++) {
if (child[j].nodeName.toLowerCase() == "td") {
child[j].style.background = "";
};
};
};
};
};
};
};
//usage:
zebra("classname");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment