table中td文字超出长度用省略号隐藏超出内容,鼠标点击内容全部显示

时间:2024-01-25 09:25:05

1,设置css样式

<style>
table {
width: 100%;
float: left;
table-layout:fixed;
width:600px;
border:1px solid #ccc;
 }

table tr {
    line-height: 25px;
    border:1px solid #ccc;
}

 table td {
     border:1px solid #ccc;
     text-align:center;
 }
.MHover{
     border:1px solid #ccc;
     white-space:nowrap;
     text-overflow:ellipsis;
     overflow:hidden;
 }
</style>

 

2,js设置click和mousemove和mouseout事件

<script>
$(document).ready(function () {
        $(".MALL").hide();
        $(".MHover").click(function (e) {
            $(this).next(".MALL").css({"position":"absolute","top":e.pageY+5,"left":e.pageX+5}).show();
        });
        $(".MHover").mousemove(function (e) {
            $(this).next(".MALL").css({ "color": "fff", "position": "absolute", "opacity": "0.7", "background-color": "666", "top": e.pageY + 5, "left": e.pageX + 5 });
        });
        $(".MHover").mouseout(function () {
            $(this).next(".MALL").hide();
        });
    });
</script>

 

3,HTML内容,在td中新增两个div,两个div中的内容必须一致。

<table>
<tr>
<th>姓名</th>
<th>个性签名</th>
<th>性别</th>
</tr>
<tr>
<td>狗子</td>
<td>
<div class="MHover">嘻嘻嘻嘻嘻哈哈12345上山打老虎嘻嘻嘻嘻嘻哈哈</div>
<div class="MALL">嘻嘻嘻嘻嘻哈哈12345上山打老虎嘻嘻嘻嘻嘻哈哈</div>
</td>
<td>男</td>
</tr>
</table>