Jquery實現(xiàn)仿新浪微博獲取文本框能輸入的字?jǐn)?shù)代碼
更新時間:2013年02月22日 16:05:00 作者:
Jquery實現(xiàn)仿新浪微博獲取文本框所能輸入的字?jǐn)?shù),感興趣的朋友可以研究一下代碼方便你折騰,希望本文提供的方法可以幫助到你
limit.js代碼
//txt:文本框jquery對象
//limit:限制的字?jǐn)?shù)
//isbyte:true:視limit為字節(jié)數(shù);false:視limit為字符數(shù)
//cb:回調(diào)函數(shù),參數(shù)為可輸入的字?jǐn)?shù)
function InitLimit(txt,limit,isbyte,cb){
txt.keyup(function(){
var str=txt.val();
var charLen;
var byteLen=0;
if(isbyte){//原文博客:blog.csdn.net/bluceyoung
for(var i=0;i<str.length;i++){
if(str.charCodeAt(i)>255){
byteLen+=2;
}else{
byteLen++;
}
}
charLen = Math.floor((limit-byteLen)/2);
}else{
byteLen=str.length;
charLen=limit-byteLen;
}
cb(charLen);
});
}
頁面代碼:
<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js" type="text/javascript">
</script>
<script type="text/javascript" src="limit.js"></script>
<script type="text/javascript">
$(document).ready(function(){
InitLimit($("#txt"),10,true,function(c){
if(c>=0){
$("#show").val("還能輸入"+c+"個字");
}else{
$("#show").val("已經(jīng)超過"+(-c)+"個字");
}
});
InitLimit($("#txt1"),10,true,function(c){
if(c>=0){
$("#show1").val("還能輸入"+c+"個字");
}else{
$("#show1").val("已經(jīng)超過"+(-c)+"個字");
}
});
});
</script>
</head>
<body>
<input type="text" id="txt"/><input id="show" type="text"/><br/>
<input type="text" id="txt1"/><input id="show1" type="text"/>
</body>
</html>
復(fù)制代碼 代碼如下:
//txt:文本框jquery對象
//limit:限制的字?jǐn)?shù)
//isbyte:true:視limit為字節(jié)數(shù);false:視limit為字符數(shù)
//cb:回調(diào)函數(shù),參數(shù)為可輸入的字?jǐn)?shù)
function InitLimit(txt,limit,isbyte,cb){
txt.keyup(function(){
var str=txt.val();
var charLen;
var byteLen=0;
if(isbyte){//原文博客:blog.csdn.net/bluceyoung
for(var i=0;i<str.length;i++){
if(str.charCodeAt(i)>255){
byteLen+=2;
}else{
byteLen++;
}
}
charLen = Math.floor((limit-byteLen)/2);
}else{
byteLen=str.length;
charLen=limit-byteLen;
}
cb(charLen);
});
}
頁面代碼:
復(fù)制代碼 代碼如下:
<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<script src="http://code.jquery.com/jquery-1.8.2.min.js" type="text/javascript">
</script>
<script type="text/javascript" src="limit.js"></script>
<script type="text/javascript">
$(document).ready(function(){
InitLimit($("#txt"),10,true,function(c){
if(c>=0){
$("#show").val("還能輸入"+c+"個字");
}else{
$("#show").val("已經(jīng)超過"+(-c)+"個字");
}
});
InitLimit($("#txt1"),10,true,function(c){
if(c>=0){
$("#show1").val("還能輸入"+c+"個字");
}else{
$("#show1").val("已經(jīng)超過"+(-c)+"個字");
}
});
});
</script>
</head>
<body>
<input type="text" id="txt"/><input id="show" type="text"/><br/>
<input type="text" id="txt1"/><input id="show1" type="text"/>
</body>
</html>

相關(guān)文章
jQuery中hover方法搭配css的hover選擇器,實現(xiàn)選中元素突出顯示方法
下面小編就為大家?guī)硪黄猨Query中hover方法搭配css的hover選擇器,實現(xiàn)選中元素突出顯示方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05jQuery動畫效果實現(xiàn)圖片無縫連續(xù)滾動
這篇文章主要為大家介紹了jQuery動畫效果實現(xiàn)圖片無縫連續(xù)滾動,實現(xiàn)類似連續(xù)不間斷的滾動廣告位,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-01-01