首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > CSS >

有一表格,用css怎么設置當鼠標停在某行時改變背景色

2012-02-13 
有一表格,用css如何設置當鼠標停在某行時改變背景色?styletype text/css tabletd,th{border:1pxsolid

有一表格,用css如何設置當鼠標停在某行時改變背景色?
<style   type= "text/css ">

table       td,th       {      
    border:1px       solid       blue;
    padding:2px   2px   2px   2px   ;
    background-color:lightgreen;
}
tr:hover   {
    background-color:#FF00CC;
}
</style>
</head>
<body>
<table>
  <tr>
    <th> 学号 </th>
    <th> 姓名 </th>
    <th> 年龄 </th>
    <th> 班级 </th>
  </tr>
  <tr>
    <td> 012 </td>
    <td> 某某 </td>
    <td> 23 </td>
    <td> 计科0102 </td>
  </tr>
  <tr   restudy= " ">
    <td> 012 </td>
    <td> 某某 </td>
    <td> 23 </td>
    <td> 计科0102 </td>
  </tr>
  <tr>
    <td> 012 </td>
    <td> 某某 </td>
    <td> 23 </td>
    <td> 计科0102 </td>
  </tr>
  <tr>
    <td> 012 </td>
    <td> 某某 </td>
    <td> 23 </td>
    <td> 计科0102 </td>
  </tr>
</table>

</body>
第八行是想讓鼠標停在某行時改變背景色為淺紅,為什麼不起作用,怎麼寫?
我用的是ie6


[解决办法]
嗯,IE6不支持这样的方式,不过如果在FF底下就可以。
如果需要让它变色,需要加js进去。参考下边这个看看~
======================================================
<!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>
<title> Basic Info </title>
<meta http-equiv= "Content-Type " content= "text/javascript; charset=gb2312 " />
<meta name= "generator " content= "editplus " />
<meta name= "author " content= "JavaScript " />
<meta name= "keywords " content= "Practise " />
<meta name= "description " content= "just a practice of the javascript " />
<style>
#wrap{
margin: 10px 50px;
}
thead{
color: #fff;
background: #1391a8;
}
tr{
line-height: 27px;
}
.sty1{
color: #1391a8;
background: #e3efef;
}
.sty2{
color: #fff;
background: #72cfd7;
}
.over{
color: #fff;
background: #ffbb1c;
}
td{
padding-left: 15px;
padding-right: 15px;
}
button{
margin: 0 10px 0 0;
}
</style>
<script>
function cc(obj){
obj.className += " over ";
}
function bc(obj){
obj.className = obj.className.replace(new RegExp( "( ?|^)over\\b "), " ");
}
</script>
</head>

<body>
<div id= "wrap ">
<form> <table>
<thead>


<td> 项目 </td>
<td> 内容 </td>
</thead>
<tbody id= "content ">
<tr class= 'sty1 ' onmouseover= "cc(this); " onmouseout= "bc(this); ">
<td> 姓名 </td>
<td> <input type= "text " size= "20 " name= "name " /> </td>
</tr>
<tr class= 'sty2 ' onmouseover= "cc(this); " onmouseout= "bc(this); ">
<td> 性别 </td>
<td>
<input type= "radio " name= "sex " value= "Male " checked= "checked "> 男 </input>
<input type= "radio " name= "sex " value= "Female "> 女 </input>
<input type= "radio " name= "sex " value= "secret "> 保密 </input>
</td>
</tr>
<tr class= 'sty1 ' onmouseover= "cc(this); " onmouseout= "bc(this); ">
<td> 生日 </td>
<td>
<select name= "birth_year ">
<option value= "blank "> --请选择年份-- </option>
</select>
<select name= "birth_month ">
<option value= "blank "> --请选择月份-- </option>
</select>
<select name= "birth_date ">
<option value= "blank "> --请选择日期-- </option>
</select>
</td>
</tr>
<tr class= 'sty2 ' onmouseover= "cc(this); " onmouseout= "bc(this); ">
<td> 语言 </td>
<td>
<input type= "checkbox " name= "eng " value= "eng "> English </input>
<input type= "checkbox " name= "chs " value= "chs " checked= "checked "> 中文 </input>
<input type= "checkbox " name= "other " value= "other "> 其他语言 </input>
</td>
</tr>
<tr class= 'sty1 ' onmouseover= "cc(this); " onmouseout= "bc(this); ">
<td> 简介 </td>
<td> <textarea name= "intro " rows= "5 " cols= "20 "> </textarea> </td>
</tr>
<tr class= 'sty2 ' onmouseover= "cc(this); " onmouseout= "bc(this); ">
<td colspan= "2 ">
<button name= "reset " onclick= " "> 清空 </button>
<button name= "sent " onclick= " "> 发送 </button>
</td>
</tr>
</tbody> </table> </form>
</div>
</body>
</html>

[解决办法]
<style type= "text/css ">

tr {background-color:lightgreen;}
td,th {
border:1px solid blue;
padding:2px 2px 2px 2px ;
}
tr.aa {background-color:#FF00CC;}
</style>
</head>
<body>
<table>
<tr onmouseover= "this.className= 'aa ' " onmouseout= "this.className= ' '; ">
<th> 学号 </th>
<th> 姓名 </th>
<th> 年龄 </th>
<th> 班级 </th>
</tr>
<tr onmouseover= "this.className= 'aa ' " onmouseout= "this.className= ' '; ">


<td> 012 </td>
<td> 某某 </td>
<td> 23 </td>
<td> 计科0102 </td>
</tr>
<tr onmouseover= "this.className= 'aa ' " onmouseout= "this.className= ' '; ">
<td> 012 </td>
<td> 某某 </td>
<td> 23 </td>
<td> 计科0102 </td>
</tr>
<tr onmouseover= "this.className= 'aa ' " onmouseout= "this.className= ' '; ">
<td> 012 </td>
<td> 某某 </td>
<td> 23 </td>
<td> 计科0102 </td>
</tr>
<tr onmouseover= "this.className= 'aa ' " onmouseout= "this.className= ' '; ">
<td> 012 </td>
<td> 某某 </td>
<td> 23 </td>
<td> 计科0102 </td>
</tr>
</table>
[解决办法]
可以用CSS解决:
在FF下直接定义 tr:hover{XXXXX}
在IE下可以定义带有JS的CSS:
onmouseover: expression(onmouseover=function (){this.style.backgroundColor = 'yellow '});

热点排行