I get this error when I try your latest code:
Code:
table has no properties
It Looks like
Code:
addBackgroundColorChange(null, "f00")
And this getElementById is not working:
Code:
addBackgroundColorChange(document.getElementById("mrTable"), "f00");
Here is the whole page:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function addBackgroundColorChange(table, backgroundColor) {
table = table.getElementsByTagName("tbody")[0];
var rows = table.getElementsByTagName("tr");
function changeColor(e) {
if(!e) e = window.event;
var o = e.target? e.target: e.srcElement;
while(o.tagName && o.tagName.toLowerCase()!="tr") o = o.parentNode;
for(var i=0, n=rows.length; i<n; ++i) {
if(!rows[i].initialBackgroundColor) rows[i].initialBackgroundColor = rows[i].style.backgroundColor;
rows[i].style.backgroundColor = rows[i].initialBackgroundColor;
if(rows[i]==o) rows[i].style.backgroundColor = backgroundColor;
}
}
if(document.addEventListener) table.addEventListener("click", changeColor, false);
else table.attachEvent("onclick", changeColor);
}
addBackgroundColorChange(document.getElementById("mrTable"), "f00");
</script>
<style type="text/css">
#mrTable {
width: 700px;
border-collapse: collapse;
}
#mrTable tr, #mrTable td {
cursor: pointer;
text-decoration: underline;
color: #00f;
}
#mrTable tr:hover {
color: #002;
}
</style>
</head>
<body>
<table id="mrTable">
<caption></caption>
<thead><tr><td></td></tr></thead>
<tfoot><tr><td></td></tr></tfoot>
<tbody>
<tr style="background-color: #ffc">
<td>
xx1xx
</td>
</tr>
<tr style="background-color: #fff">
<td>
xx2xx
</td>
</tr>
<tr style="background-color: #ffc">
<td>
xx3xx
</td>
</tr>
</tbody>
</table>
</body>
</html>
Bookmarks