textarea输入限制+字数统计(JQuery版),练习JQuery

4623阅读 0评论2011-07-21 qyf404
分类:系统运维

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

  2. <html>
  3. <head>
  4. <title>Untitled</title>
  5. <script src="jquery-1.3.2.js"></script>
  6. <script src="jquery-1.3.2.min.js"></script>
  7. <script language="javascript">
  8. $(function(){
  9.     jQuery.fn.extend({
  10.         showWordCount: function() {
  11.             var _max = $(this).attr('max');
  12.             var _length = $(this).text().length;
  13.             if(_length > _max) {
  14.                 $(this).text($(this).text().substring(0, _max));
  15.             }
  16.             _left = $(this).offset().left;
  17.             _top = $(this).offset().top;
  18.             _width = $(this).width();
  19.             _height = $(this).height();
  20.             $('#div_1').html(_length + '/' + _max);
  21.             $('#div_1').css({
  22.                 'left':_left + _width - 60,
  23.                 'top':_top + _height - 8
  24.             });
  25.         }
  26.     });
  27.     
  28.     $('textarea').bind('propertychange', function() {
  29.         $(this).showWordCount();
  30.     });
  31.     
  32.     $('textarea').focus(function(){
  33.         $(this).showWordCount();
  34.         $('#div_1').fadeIn('slow');
  35.     });
  36.     
  37.     $('textarea').blur(function(){
  38.            $('#div_1').fadeOut('slow');
  39.     });
  40. });
  41. </script>
  42. <style>
  43. .div_1 {
  44.     position:absolute;
  45.     font-size:9pt;
  46.     color:#4586b5;
  47.     width:50px;
  48.     text-align:right;
  49.     display:none
  50. }
  51. </style>
  52. </head>

  53. <body>
  54. <center>
  55. <textarea id="textarea_1" cols=50 rows=10 max="100">123</textarea>
  56. <div id="div_1" class="div_1">123</div>
  57. </center>
  58. </body>
  59. </html>


上一篇:翻页的策略
下一篇:介绍一个十分好用的JQUERY图片放大镜插件