用jquery 实现 超出字符 截断加上省略号并且可以提示全部内容

时间:2023-03-09 08:01:30
用jquery 实现 超出字符 截断加上省略号并且可以提示全部内容

1.test.jsp

  Java代码

 <%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="${webRoot}/framework/js/jquery-1.9.1.js"></script>
<script type="text/javascript" src="${webRoot}/framework/js/jquery-ui.js"></script>
<script type="text/javascript" src="${webRoot}/framework/js/strlimit.js"></script>
<script>
$('table').tooltip();
</script>
</head>
<table>
<tr>
<td class="centerContentTd" nowrap="nowrap" title="这里是全部内容" limit="8">这里显示截取后的内容
长度大于8将用省略号代替</td>
</tr>
</table>
</html>

2.strlimit.js

Javascript代码

 jQuery.fn.limit=function(){
var self = $("[limit]");
self.each(function(){
var objString = $(this).text();
var objLength = $(this).text().length;
var num = $(this).attr("limit");
if(objLength > num){
$(this).attr("title",objString);
objString = $(this).text(objString.substring(0,num) + "...");
}
})
}
$(function(){
$("[limit]").limit();
})