Предлагаю следующую реализацию (все в одной функции):
<script type="text/javascript">
var superF = (function(){
var storage;
return function(){
var event = arguments[0] || window.event
switch(event.type){
case 'click':
if (storage && storage != this) storage.style.background = "white";
this.style.background = "green";
storage = this;
break;
case 'mouseout':
if (storage != this) this.style.background = "white";
break;
case 'mouseover':
if (storage != this) this.style.background = "#39F";
break;
};
};
}());
var n = 3;
document.write("<table border='1' style='border-collapse:collapse; width:400px; height:400px;'>");
for (var tr = 1; tr <= n; tr++) {
document.write("<tr>");
for (var td = 1; td <= n; td++) {
document.write("<td align='center' valign='middle' onclick='superF.call(this)' onmouseout='superF.call(this)' onmouseover='superF.call(this)' id='tdcolor'>A</td>")};
document.write("</tr>")
}
document.write("</table>");
</script>
UPD: подправил передачу события