JS表格点击每一行颜色变化的问题
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>homework1.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
window.onload = function(){
var tr = document.getElementsByTagName("tr");
for(var i=1;i<tr.length;i++){
tr[i].onclick = function(){
changecolor(this);
}
}
}
function changecolor(obj){
obj.style.backgroundColor= "red";
}
</script>
</head>
<body>
<table border="1" style="background-color: blue">
<tr><td>商品名</td><td>单价</td><td>数量</td></tr>
<tr><td>蛋糕</td><td>20</td><td>3</td></tr>
<tr><td>葡萄</td><td>12</td><td>2</td></tr>
<tr><td>苹果</td><td>10</td><td>4</td></tr>
<tr><td>芒果</td><td>19</td><td>5</td></tr>
<tr><td>荔枝</td><td>8</td><td>2</td></tr>
</table>
</body>
</html>
上面这个代码 鼠标点击一次 哪一行的背景颜色变成红色 然后点另一行的时候 原来点的哪一行 变回表格原来的颜色 而现在点的这一行变成红色 怎么实现
不用鼠标进入和离开的事件 只用onclick事件
------解决方案--------------------
HTML code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>homework1.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
<script type="text/javascript">
window.onload = function(){
var cur;
var tr = document.getElementsByTagName("tr");
for(var i=1;i<tr.length;i++){
tr[i].onclick = function(){
cur ? cur.style.backgroundColor = 'blue' : '';
changecolor(this);
cur = this;
}
}
}
function changecolor(obj){
obj.style.backgroundColor= "red";
}
</script>
</head>
<body>
<table border="1" style="background-color: blue">
<tr><td>商品名</td><td>单价</td><td>数量</td></tr>
<tr><td>蛋糕</td><td>20</td><td>3</td></tr>
<tr><td>葡萄</td><td>12</td><td>2</td></tr>
<tr><td>苹果</td><td>10</td><td>4</td></tr>
<tr><td>芒果</td><td>19</td><td>5</td></tr>
<tr><td>荔枝</td><td>8</td><td>2</td></tr>
</table>
</bod