JQuery擷取文字框中字元長度的代碼

來源:互聯網
上載者:User

趁我寫例子這點時間有兩個人回答了 呵呵
剛才實驗過了 看下例子吧
JS 方法:
複製代碼 代碼如下:
<script type="text/javascript">
// 得到字串的真實長度(雙位元組換算為兩個單位元組)
function getStrActualLen(sChars)
{
//sChars.replace(/[^\x00-\xff]/g,"xx").length/1024+"位元組";
//Math.round(sChars.replace(/[^\x00-\xff]/g,"xx").length/1024);這個貌似不好使
return alert(formatNum(sChars.replace(/[^\x00-\xff]/g,"xx").length/1024,4));
}
//格式化小數,並四捨五入。如:formatNum(100.12345678,4)
function formatNum(Num1,Num2){
if(isNaN(Num1)||isNaN(Num2)){
return(0);
}else{
Num1=Num1.toString();
Num2=parseInt(Num2);
if(Num1.indexOf('.')==-1){
return(Num1);
}else{
var b=Num1.substring(0,Num1.indexOf('.')+Num2+1);
var c=Num1.substring(Num1.indexOf('.')+Num2+1,Num1.indexOf('.')+Num2+2);
if(c==""){
return(b);
}else{
if(parseInt(c)<5){
return(b);
}else{
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));
}
}
}
}
}

Jquery方法:
複製代碼 代碼如下:
function getStrActualLen(){
var count=$("#sChars").val().length/1024;
return Math.round(count*Math.pow(10,4));
}

jquery 截取字串長度
jquery 可以用text()方法來取得字串,再用length 判斷長度,然後用substring()方法格式化截斷後加'...'省略符號。
複製代碼 代碼如下:
<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312' />
<title>cookie</title>
<script src='http://img.jb51.net/jslib/jquery/jquery-1.3.2.min.js' type='text/javascript'></script>
<style>
*{ margin:0; padding:0;font-family:'宋體',Arial, Helvetica, sans-serif;}
#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}
.blank{ font-size:18px; font-weight:bold; text-align:center; padding:20px}
</style>
<script type='text/javascript'>
jQuery.fn.limit=function(){
var self = $('div[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(){
$(document.body).limit();
})
</script>
</head>
<body>
<div class='blank'>請按F5重新整理頁面。。。。</div>
<div id='best'>
<div limit='12'>字串字串字串字串字串字串字串字串字串</div>
<div limit='10'>字串字串字串字串字串字串字串</div>
<div limit='12'>字串字串字串字串字串</div>
<div limit='12'>字串字串字串字串字串字串字串字串</div>
<div limit='10'>字串字串字串字串字串字串字串</div>
</div>
</body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.