Search

11/18/2006

Using DOM Scripting to Plug the Holes in CSS

Using DOM Scripting to Plug the Holes in CSS

HTML: Structure
CSS: Presentation
DOM Scripting: Behaviour


document.getElementsByClassName = function(name) {
var results = new Array();
var elems = document.getElementsByTagName("*");
for (var i=0; i<elems.length; i++) {
if (elems[i].className.indexOf(name) != -1) {
results[results.length] = elems[i];
}
}
return results;
};

function stripeLists() {
var lists = document.getElementsByTagName("ol");
for (var i=0; i<lists.length; i++) {
if (lists[i].className.match("striped")) {
var items = lists[i].getElementsByTagName("li");
for (var j=0; j<items.length; j=j+2) {
addClass(items[j],"odd");
}
}
}
}

tr:nth-child(2n+1) 
/* represents every odd row of an HTML table */
tr:nth-child(odd) /* same */
tr:nth-child(2n) /* represents every even row of an HTML table */
tr:nth-child(even) /* same */

沒有留言: