鼠标悬浮表格行变色代码

快乐打工仔 分类:实例代码

鼠标悬浮表格行变色代码属于前端实例代码,有关更多实例代码大家可以查看

table表格是组织数据的利器,本文分享一个十分常见的鼠标效果,那就是鼠标悬浮可以实现当前行背景变色。

代码实例如下:

<!DOCTYPE html>  
<html>  
<head>  
<meta charset=" utf-8">  
<meta name="author" content="http://www.pipipi.net/" />  
<title>前端教程网</title>  
<style type="text/css"> 
.table{ 
  width:300px; 
  height:100px; 
  border:1px solid #ccc; 
  border-collapse:collapse; 
} 
.table td,.table th { 
  border:1px solid #ccc; 
  padding:5px; 
} 
.hover{
  background:#CCC;
}
</style> 
<script src="https://code.jquery.com/jquery-3.0.0.js"></script>
<script type="text/javascript"> 
$(document).ready(function(){
  $(".table tr").hover(function(){
    $(this).children("td").addClass("hover")
  }, function(){
    $(this).children("td").removeClass("hover")
  })
})
</script> 
</head> 
<body> 
<table class="table"> 
  <thead> 
    <tr> 
      <th>前端教程网一</th> 
      <th>前端教程网二</th> 
    </tr> 
  </thead> 
  <tr> 
    <td>javascript教程</td> 
    <td>jQuery教程</td> 
  </tr> 
  <tr> 
    <td>HTML教程</td> 
    <td>div css教程</td> 
  </tr> 
</table> 
</body> 
</html>

当鼠标悬浮的时候,能够实现背景变色效果,代码非常的简单。

相关阅读:

(1).hover()参阅jQuery hover事件一章节。 

(2).children()参阅jQuery children()一章节。 

(3).addClass()参阅jQuery addClass()一章节。 

鼠标悬浮表格行变色代码,这样的场景在实际项目中还是用的比较多的,关于鼠标悬浮表格行变色代码就介绍到这了。

回复

我来回复
  • 暂无回复内容