Pure cross-browser Javascript with working example:
var bgColor;
var els = document.getElementsByTagName('*');
for (var i = 0; i < els.length; i++) {
if (document.addEventListener) {
els[i].addEventListener('mouseover', function (e) {
e.stopPropagation();
bgColor = '#' + Math.floor(Math.random() * 16777215).toString(16);
this.style.backgroundColor = bgColor;
}, false);
els[i].addEventListener('mouseout', function (e) {
e.stopPropagation();
bgColor = '#FFFFFF';
this.style.backgroundColor = bgColor;
}, false);
} else {
els[i].attachEvent('mouseover', function () {
e.stopPropagation();
bgColor = '#' + Math.floor(Math.random() * 16777215).toString(16);
this.style.backgroundColor = bgColor;
});
els[i].attachEvent('mouseout', function () {
e.stopPropagation();
bgColor = '#FFFFFF';
this.style.backgroundColor = bgColor;
});
}
}
Random background code from here: http://paulirish.com/2009/random-hex-color-code-snippets/
getRandom(), which returns a random color.