趁我写例子这点时间有两个人回答了 呵呵 刚才试验过了 看下例子吧 JS 方法: 复制代码 代码如下: <br>// 得到字符串的真实长度(双字节换算为两个单字节) <br>function getStrActualLen(sChars) <br>{ <br>//sChars.replace(/[^x00-xff]/g,"xx").length/1024 "字节"; <br>//Math.round(sChars.replace(/[^x00-xff]/g,"xx").length/1024);这个貌似不好使 <br>return alert(formatNum(sChars.replace(/[^x00-xff]/g,"xx").length/1024,4)); <br>} <br>//格式化小数,并四舍五入。如:formatNum(100.12345678,4) <br>function formatNum(Num1,Num2){ <br>if(isNaN(Num1)||isNaN(Num2)){ <br>return(0); <br>}else{ <br>Num1=Num1.toString(); <br>Num2=parseInt(Num2); <br>if(Num1.indexOf('.')==-1){ <br>return(Num1); <br>}else{ <br>var b=Num1.substring(0,Num1.indexOf('.') Num2 1); <br>var c=Num1.substring(Num1.indexOf('.') Num2 1,Num1.indexOf('.') Num2 2); <br>if(c==""){ <br>return(b); <br>}else{ <br>if(parseInt(c)<5){ <BR>return(b); <BR>}else{ <BR>return((Math.round(parseFloat(b)*Math.pow(10,Num2)) Math.round(parseFloat(Math.pow(0.1,Num2).toString().substring(0,Math.pow(0.1,Num2).toString().indexOf('.') Num2 1))*Math.pow(10,Num2)))/Math.pow(10,Num2)); <BR>} <BR>} <BR>} <BR>} <BR>} <BR></div> <BR>Jquery方法: <BR><div class="codetitle"><span><a style="CURSOR: pointer" data="97850" class="copybut" id="copybut97850" onclick="doCopy('code97850')"><U>复制代码</U></a></span> 代码如下:</div><div class="codebody" id="code97850"> <BR>function getStrActualLen(){ <BR>var count=$("#sChars").val().length/1024; <BR>return Math.round(count*Math.pow(10,4)); <BR>} <BR></div><BR>jquery 截取字符串长度 <BR>jquery 可以用text()方法来取得字符串,再用length 判断长度,然后用substring()方法格式化截断后加'...'省略号。 <BR><div class="codetitle"><span><a style="CURSOR: pointer" data="75824" class="copybut" id="copybut75824" onclick="doCopy('code75824')"><U>复制代码</U></a></span> 代码如下:</div><div class="codebody" id="code75824"> <BR><!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> <br><html xmlns='http://www.w3.org/1999/xhtml'> <br><head> <br><meta http-equiv='Content-Type' content='text/html; charset=gb2312' /> <br><title>cookie</title> <br><script src='http://img.jb51.net/jslib/jquery/jquery-1.3.2.min.js' type='text/javascript'> <br>*{ margin:0; padding:0;font-family:'宋体',Arial, Helvetica, sans-serif;} <br>#best{ width:300px; height:200px; border:1px solid #ccc; margin:60px auto 0; line-height:1.6; font-size:14px; padding:10px 0 0 10px} <br>.blank{ font-size:18px; font-weight:bold; text-align:center; padding:20px} <br> <br>jQuery.fn.limit=function(){ <br>var self = $('div[limit]'); <br>self.each(function(){ <br>var objString = $(this).text(); <br>var objLength = $(this).text().length; <br>var num = $(this).attr('limit'); <br>if(objLength > num){ <br>$(this).attr('title',objString); <br>objString = $(this).text(objString.substring(0,num) '...'); <br>} <br>}) <br>} <br>$(function(){ <br>$(document.body).limit(); <br>}) <br> 请按F5刷新页面。。。。 字符串字符串字符串字符串字符串字符串字符串字符串字符串 字符串字符串字符串字符串字符串字符串字符串 字符串字符串字符串字符串字符串 字符串字符串字符串字符串字符串字符串字符串字符串 字符串字符串字符串字符串字符串字符串字符串