当前位置:编程学习 > html/css >>

HTML中table的高亮以及tooltip

在一个需要显示很多数据的表格(table)中,为了更友好地查看一行数据,常常需要在鼠标指针移到某一行时,高亮此行。要实现这个效果有很多方法,这里列举一个方法:

function setTableHover(t) {
  $(t + " tbody tr")
      .mouseover(function() { $(this).addClass("hover");})
      .mouseout(function() { $(this).removeClass("hover"); })
}
主要就是在鼠标移到某一行时,为该行添加一个高亮的css class,鼠标离开时移除该class即可。可以为一个特定的table设定:

<table id="test">
</table>
<script>
    setTableHover('#test')
</script>
甚至可以为将某个页面的所有table设为高亮:

<script>
    setTableHover('table')
</script>
css里需要编写这个hover:

.hover {
  background: #e9cffa;
}
除了高亮显示某一行外,可能还需要在鼠标移动到某个单元格时,弹出一个tooltip。这里的tooltip可以是弹出窗口,也就是一个div元素。

<tr>
  <td class="tip">
    hello
    <div class='popup' style='display:none;'>this is the tip</div>
  </td>
</tr>
要实现此效果,可以通过修改包含tip class的鼠标事件响应:

$(function () {
    $('.tip').hover(
        function () {
            show_popupex($(this).find("div"), $(this));
        },
        function () {
            $(this).find("div").hide();
        }
        );
});
 

补充:web前端 , HTML 5 ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,