CSS如何实现把鼠标放在行上整行变色?

CSS如何实现把鼠标放在行上整行变色:

在很多网站都有这样的效果,那就是当鼠标放在一个文章列表行的时候,此行就会显示与其他行不同的颜色,本站的文章列表也具有这样的效果,便于浏览者识别,非常人性化,下面就简单介绍一下如何实现此效果。代码实例如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.51texiao.cn/" />
<title>蚂蚁部落</title>
<style type="text/css">
a{text-decoration:none;}
li:hover{background-color:green;}
</style>
</head>
<body>
<ul>
  <li><a href="#">html</a></li>
  <li><a href="#">div+css</a></li>
  <li><a href="#">javascript</a></li>
  <li><a href="#">Jquery</a></li>
</ul>
</body>
</html>

以上代码通过使用E:hover伪类选择器实现了此效果。

但是此中方法有个缺点,就是IE6浏览器不支持除去a元素之外的E:hover伪类选择器。下面介绍一下能够兼容所有浏览器的方法,代码实例如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.51texiao.cn/" />
<title>蚂蚁部落</title>
<style type="text/css">
a{text-decoration:none;}
.over{background-color:green;}
.out{background-color:#FFF;}
</style>
</head>
<body>
<ul>
  <li onmouseover="this.className='over'" onmouseout="this.className='out'">
    <a href="#">html</a>
  </li>
  <li onmouseover="this.className='over'" onmouseout="this.className='out'">
    <a href="#">div+css</a>
  </li>
  <li onmouseover="this.className='over'" onmouseout="this.className='out'">
    <a href="#">javascript</a>
  </li>
  <li onmouseover="this.className='over'" onmouseout="this.className='out'">
    <a href="#">Jquery</a>
  </li>
</ul>
</body>
</html>
使用纯CSS实现在鼠标经过一个表格的某一行的时候,要整行的背景颜色发生变化,以表明该行正中焦点:

<html>

<head>

<metacharset="utf-8"/>

<title>change</title>

<styletype="text/css">

tr.change:hover

{

margin: 0px !important; padding: 0px 1em !important; outline: 0px !important; border-radius: 0px !important; border: 0px !important; bottom: auto !important; float: none !important; height: auto !important; left: auto !important; overflow: visible !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important; box-sizing: content-box !important; min-height: auto !important; white-space: pre !important; background-image: none !important; background-attachment: initial !important; background-size: initial !important; background-origin: initial !important; background-clip: initial !important; background-position: initial !important; background-repeat: initial !important;"> }

</style>

</head>

<body>

<h1>鼠标经停变色</h1>

<tablewidth="80%"border="1"align="center">

<trclass="change">

<td>&nbsp;</td>

<td>&nbsp;</td>

</tr>

<trclass="change">

<td>&nbsp;</td>

<td>&nbsp;</td>

</tr>

</table>

</body>

</html>

以上所述是小编给大家介绍的HTML中实现鼠标经停时整行(tr)变色,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!