1.test.jsp
Java代码
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 5 <script type="text/javascript" src="${webRoot}/framework/js/jquery-1.9.1.js"></script> 6 <script type="text/javascript" src="${webRoot}/framework/js/jquery-ui.js"></script> 7 <script type="text/javascript" src="${webRoot}/framework/js/strlimit.js"></script> 8 <script> 9 $('table').tooltip(); 10 </script> 11 </head> 12 <table> 13 <tr> 14 <td class="centerContentTd" nowrap="nowrap" title="这里是全部内容" limit="8">这里显示截取后的内容 15 长度大于8将用省略号代替</td> 16 </tr> 17 </table> 18 </html>
2.strlimit.js
Javascript代码
1 jQuery.fn.limit=function(){ 2 var self = $("[limit]"); 3 self.each(function(){ 4 var objString = $(this).text(); 5 var objLength = $(this).text().length; 6 var num = $(this).attr("limit"); 7 if(objLength > num){ 8 $(this).attr("title",objString); 9 objString = $(this).text(objString.substring(0,num) + "..."); 10 } 11 }) 12 } 13 $(function(){ 14 $("[limit]").limit(); 15 })