标签:color io ar for strong sp div art 问题
textbox如果设置TextMode="MultiLine"则 它的MaxLength设置的值就无效;为了能达到像腾讯微薄、新浪微薄那样的提示的效果(腾讯和新浪微薄文本框用到的应该是textarea),尝试如果不考虑用鼠标操作粘贴、删除textbox的内容,用jquery的keyup和keydown能实现,下面是实现的一个技巧,用到了js的计时器(当焦点在textbox中则“开启”计时器,失去焦点则“关闭”计时器),很好的解决了鼠标操作粘贴、删除textbox的内容不能改变字符个数的问题
<script> |
02 |
|
03 |
var t = "" ; |
04 |
function maxLimit() { |
05 |
if ($.trim($( "#txtContent" ).val()).length > 140) { |
06 |
$( "#txtleft" ).text( "已经超出" ); |
07 |
$( "#LabelContent" ).text(($.trim($( "#txtContent" ).val()).length) - 140); |
08 |
} |
09 |
else { |
10 |
$( "#txtleft" ).text( "还能输入" ); |
11 |
$( "#LabelContent" ).text(140 - ($.trim($( "#txtContent" ).val()).length)); |
12 |
} |
13 |
} |
14 |
|
15 |
function setTimeouts() { |
16 |
maxLimit(); |
17 |
t = setTimeout( "setTimeouts()" , 1 * 10); |
18 |
}; |
19 |
|
20 |
function clearTimeouts() { |
21 |
clearTimeout(t); |
22 |
}; |
23 |
|
24 |
|
25 |
$(document).ready( function () { |
26 |
|
27 |
//$("#txtContent").keyup(maxLimit); |
28 |
//$("#txtContent").keydown(maxLimit); |
29 |
$( "#txtContent" ).bind( "blur" , clearTimeouts); |
30 |
$( "#txtContent" ).bind( "focus" , setTimeouts) |
31 |
}); |
32 |
|
33 |
</script> |
< div > < asp:TextBox ID = "txtContent" runat = "server" Width = "500px" TextMode = "MultiLine" MaxLength = "140" |
Height = "100px" ></ asp:TextBox ></ div > |
|
< div >< span id = "txtleft" >还能输入</ span >< asp:Label ID = "LabelContent" runat = "server" ForeColor = "Red" |
Text = "140" ></ asp:Label >< span >个字符</ span ></ div > |
标签:color io ar for strong sp div art 问题
原文地址:http://www.cnblogs.com/ranzige/p/4013820.html